Commit Graph

1108 Commits

Author SHA1 Message Date
kassens c53145cf89 Remove enableRefAsProp feature flag (#30346)
The flag is fully rolled out.

DiffTrain build for [07aa494432](https://github.com/facebook/react/commit/07aa494432e97f63fca9faf2fad6f76fead31063)
2024-11-04 11:38:26 -08:00
yungsters eeb33289ca Fix Ref Lifecycles in Hidden Subtrees (#31379)
## Summary

We're seeing certain situations in React Native development where ref
callbacks in `<Activity mode="hidden">` are sometimes invoked exactly
once with `null` without ever being called with a "current" value.

This violates the contract for refs because refs are expected to always
attach before detach (and to always eventually detach after attach).
This is *particularly* bad for refs that return cleanup functions,
because refs that return cleanup functions expect to never be invoked
with `null`. This bug causes such refs to be invoked with `null`
(because since `safelyAttachRef` was never called, `safelyDetachRef`
thinks the ref does not return a cleanup function and invokes it with
`null`).

This fix makes use of `offscreenSubtreeWasHidden` in
`commitDeletionEffectsOnFiber`, similar to how
https://github.com/facebook/react/commit/ec52a5698e2dfea7050a0b015f0b79abfb2d81b7
did this for `commitDeletionEffectsOnFiber`.

## How did you test this change?

We were able to isolate the repro steps to isolate the React Native
experimental changes. However, the repro steps depend on Fast Refresh.

```
function callbackRef(current) {
  // Called once with `current` as null, upon triggering Fast Refresh.
}

<Activity mode="hidden">
  <View ref={callbackRef} />;
</Activity>
```

Ideally, we would have a unit test that verifies this behavior without
Fast Refresh. (We have evidence that this bug occurs without Fast
Refresh in real product implementations. However, we have not
successfully deduced the root cause, yet.)

This PR currently includes a unit test that reproduces the Fast Refresh
scenario, which is also demonstrated in this CodeSandbox:
https://codesandbox.io/p/sandbox/hungry-darkness-33wxy7

Verified unit tests pass:

```
$ yarn
$ yarn test
# Run with `-r=www-classic` for `enableScopeAPI` tests.
$ yarn test -r=www-classic
```

Verified on the internal React Native development branch that the bug no
longer repros.

---------

Co-authored-by: Rick Hanlon <rickhanlonii@fb.com>

DiffTrain build for [ea3ac58669](https://github.com/facebook/react/commit/ea3ac586693014e882655728fc8396ecb1d6cf6e)
2024-10-31 13:32:06 -07:00
sebmarkbage 0fb655b159 Capture the source and not just the stack on first seen error (#31367)
Otherwise we can't capture the owner stack at the right location when
there's a rethrow.

DiffTrain build for [0bc3074873](https://github.com/facebook/react/commit/0bc30748730063e561d87a24a4617526fdd38349)
2024-10-28 14:07:10 -07:00
jackpope 2ef3590635 Revert "[Re-land] Make prerendering always non-blocking (#31268)" (#31355)
This reverts commit 6c4bbc7832.

It looked like the bug we found on the original land was related to
broken product code. But through landing #31268 we found additional bugs
internally. Since disabling the feature flag does not fix the bugs, we
have to revert again to unblock the sync. We can continue to debug with
our internal build.

DiffTrain build for [cae764ce81](https://github.com/facebook/react/commit/cae764ce81b1bd6c418e9e23651794b6b09208e8)
2024-10-25 09:24:35 -07:00
SamChou19815 bd06b969c2 [flow] Eliminate usage of more than 1-arg React.AbstractComponent in React codebase (#31314)
<!--
  Thanks for submitting a pull request!
We appreciate you spending the time to work on these changes. Please
provide enough information so that others can review your pull request.
The three fields below are mandatory.

Before submitting a pull request, please make sure the following is
done:

1. Fork [the repository](https://github.com/facebook/react) and create
your branch from `main`.
  2. Run `yarn` in the repository root.
3. If you've fixed a bug or added code that should be tested, add tests!
4. Ensure the test suite passes (`yarn test`). Tip: `yarn test --watch
TestName` is helpful in development.
5. Run `yarn test --prod` to test in the production environment. It
supports the same options as `yarn test`.
6. If you need a debugger, run `yarn test --debug --watch TestName`,
open `chrome://inspect`, and press "Inspect".
7. Format your code with
[prettier](https://github.com/prettier/prettier) (`yarn prettier`).
8. Make sure your code lints (`yarn lint`). Tip: `yarn linc` to only
check changed files.
  9. Run the [Flow](https://flowtype.org/) type checks (`yarn flow`).
  10. If you haven't already, complete the CLA.

Learn more about contributing:
https://reactjs.org/docs/how-to-contribute.html
-->

## Summary

In order to adopt react 19's ref-as-prop model, Flow needs to eliminate
all the places where they are treated differently.
`React.AbstractComponent` is the worst example of this, and we need to
eliminate it.

This PR eliminates them from the react repo, and only keeps the one that
has 1 argument of props.

## How did you test this change?

yarn flow

DiffTrain build for [45804af18d](https://github.com/facebook/react/commit/45804af18d589fd2c181f3b020f07661c46b73ea)
2024-10-21 16:26:07 -07:00
sebmarkbage 750a0e4d65 Audit try/finally around console patching (#31286)
Otherwise if something errors they can be left patched.

[Review without
whitespace](https://github.com/facebook/react/pull/31286/files?w=1)

DiffTrain build for [b8ae38f88b](https://github.com/facebook/react/commit/b8ae38f88b70f8a0ea96421a4355266aafefee7f)
2024-10-18 09:14:24 -07:00
yungsters d4ef1bcd0c Delete __SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED from React Native Renderer (#31276)
## Summary

The React Native Renderer exports a
`__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED` property with a
single method that has no remaining call sites:
`computeComponentStackForErrorReporting`

This PR cleans up this unused export.

## How did you test this change?

```
$ yarn
$ yarn flow fabric
$ yarn test
```

DiffTrain build for [a3d9ea05bf](https://github.com/facebook/react/commit/a3d9ea05bf01f3c3d7aedc2d938c581ad11fd14a)
2024-10-16 11:27:56 -07:00
jackpope 6ee7d9d904 [Re-land] Make prerendering always non-blocking (#31268)
Follows https://github.com/facebook/react/pull/31238

___

This is a partial re-land of
https://github.com/facebook/react/pull/31056. We saw breakages surface
after the original land and had to revert. Now that they've been fixed,
let's try this again. This time we'll split up the commits to give us
more control of testing and rollout internally.

Original PR: https://github.com/facebook/react/pull/31056
Original Commit:
https://github.com/facebook/react/pull/31056/commits/4c71025d8d1bd46344ad793e7ed3049d24f7395a
Revert PR: https://github.com/facebook/react/pull/31080

Commit description:

> When a synchronous update suspends, and we prerender the siblings, the
prerendering should be non-blocking so that we can immediately restart
once the data arrives.
>
> This happens automatically when there's a Suspense boundary, because
we immediately commit the boundary and then proceed to a Retry render,
which are always concurrent. When there's not a Suspense boundary, there
is no Retry, so we need to take care to switch from the synchronous work
loop to the concurrent one, to enable time slicing.

Co-authored-by: Andrew Clark <git@andrewclark.io>

DiffTrain build for [6c4bbc7832](https://github.com/facebook/react/commit/6c4bbc783286bf6eebd9927cb52e8fec5ad4dd74)
2024-10-15 13:55:45 -07:00
jackpope 01ac93feaa [Re-land] Make prerendering always non-blocking: Add missing feature flag checks (#31238)
This is a partial re-land of
https://github.com/facebook/react/pull/31056. We saw breakages surface
after the original land and had to revert. Now that they've been fixed,
let's try this again. This time we'll split up the commits to give us
more control of testing and rollout internally.

Original PR: https://github.com/facebook/react/pull/31056
Original Commit:
https://github.com/facebook/react/pull/31056/commits/2a9fb445d98b60a97f3642cec2ff22469727e0c7
Revert PR: https://github.com/facebook/react/pull/31080

Commit description:
```
Neglected to wrap some places in the enableSiblingPrerendering flag.
```

Co-authored-by: Andrew Clark <git@andrewclark.io>

DiffTrain build for [13411e4589](https://github.com/facebook/react/commit/13411e4589f3d999727c5322781e2dd7bef3b256)
2024-10-14 11:19:44 -07:00
kassens f1147578e3 [cleanup] remove flag enableAddPropertiesFastPath (#31062)
The experiment was tested internally and rolled out, replacing the flag
with `true`.

DiffTrain build for [de43d560a8](https://github.com/facebook/react/commit/de43d560a8622fa44ba4162424437125564e906e)
2024-10-09 13:55:04 -07:00
poteto b3b172a470 [rcr] Re-export useMemoCache in top level React namespace (#31139)
In order to support using the compiler on versions of React prior to 19,
we need the ability to statically import `c` (aka useMemoCache) or
fallback to a polyfill supplied by `react-compiler-runtime` (note: this
is a separate npm package, not to be confused with
`react/compiler-runtime`, which is currently a part of react).

To do this we first need to re-export `useMemoCache` under the top level
React namespace again, which is additive and thus non-breaking. Doing so
allows `react-compiler-runtime` to statically either re-export
`React.__COMPILER_RUNTIME.c` or supply a polyfill, without the need for
a dynamic import which is finicky to support due to returning a promise.

In later PRs I will remove `react/compiler-runtime` and update the
compiler to emit imports to `react-compiler-runtime` instead.

DiffTrain build for [b78a7f2f35](https://github.com/facebook/react/commit/b78a7f2f35e554a8647c3262d7f392e68d06febc)
2024-10-07 13:32:16 -07:00
yungsters 6acd66ee67 Define HostInstance type for React Native (#31101)
## Summary

Creates a new `HostInstance` type for React Native, to more accurately
capture the intent most developers have when using the `NativeMethods`
type or `React.ElementRef<HostComponent<T>>`.

Since `React.ElementRef<HostComponent<T>>` is typed as
`React.AbstractComponent<T, NativeMethods>`, that means
`React.ElementRef<HostComponent<T>>` is equivalent to `NativeMethods`
which is equivalent to `HostInstance`.

## How did you test this change?

```
$ yarn
$ yarn flow fabric
```

DiffTrain build for [459fd418cf](https://github.com/facebook/react/commit/459fd418cfbd1f2f1be58efd8c89a0e0ecfb6d44)
2024-10-01 17:33:10 -07:00
jackpope 799b307e65 Disable infinite render loop detection (#31088)
We're seeing issues with this feature internally including bugs with
sibling prerendering and errors that are difficult for developers to
action on. We'll turn off the feature for the time being until we can
improve the stability and ergonomics.

This PR does two things:
- Turn off `enableInfiniteLoopDetection` everywhere while leaving it as
a variant on www so we can do further experimentation.
- Revert https://github.com/facebook/react/pull/31061 which was a
temporary change for debugging. This brings the feature back to
baseline.

DiffTrain build for [d8c90fa48d](https://github.com/facebook/react/commit/d8c90fa48d3addefe4b805ec56a3c65e4ee39127)
2024-10-01 08:08:35 -07:00
gnoff 342e45c4c0 [Fizz] Start initial work immediately (#31079)
In a recent update we make Flight start working immediately rather than
waitin for a new task. This commit updates fizz to have similar
mechanics. We start the render in the currently running task but we do
so in a microtask to avoid reentrancy. This aligns Fizz with Flight.

ref: https://github.com/facebook/react/pull/30961

DiffTrain build for [67fee58b1f](https://github.com/facebook/react/commit/67fee58b1f72754cc77488c40c44e786572ef954)
2024-09-26 13:59:28 -07:00
poteto f8028b9b9b No commit message
DiffTrain build for [67fee58b1f](https://github.com/facebook/react/commit/67fee58b1f72754cc77488c40c44e786572ef954)
2024-09-26 13:58:43 -07:00
jackpope b2fb6f5503 Turn on lazy context propagation for RN and RTR (#31076)
Following https://github.com/facebook/react/pull/30935 let's turn this
on across the board so we can clean up experiments in RN.

DiffTrain build for [60b1420f18](https://github.com/facebook/react/commit/60b1420f18e9473230f23f88fdc7b89fea3dbbfd)
2024-09-26 10:04:13 -07:00
hoxyq 635b8b6288 fix: use public instance in Fiber renderer and expose it from getInspectorDataForViewAtPoint (#31068)
React DevTools no longer operates with just Fibers, it now builds its
own Shadow Tree, which represents the tree on the Host (Fabric on
Native, DOM on Web).

We have to keep track of public instances for a select-to-inspect
feature. We've recently changed this logic in
https://github.com/facebook/react/pull/30831, and looks like we've been
incorrectly getting a public instance for Fabric case.

Not only this, turns out that all `getInspectorData...` APIs are
returning Fibers, and not public instances. I have to expose it, so that
React DevTools can correctly identify the element, which was selected.

Changes for React Native are in
[D63421463](https://www.internalfb.com/diff/D63421463)

DiffTrain build for commit https://github.com/facebook/react/commit/d66fa02a303fc53d901bdb0d7bbdaec3e6774b19.
2024-09-26 02:25:42 -07:00
eps1lon 2baa9b403a [Fiber] Fix missing render times when we cancel a pending commit (#31065)
DiffTrain build for commit https://github.com/facebook/react/commit/778e1ed2e5ec22d4bac48e14167d3b4a6b28e8b8.
2024-09-25 15:28:46 -07:00
acdlite 64e263fc13 Make prerendering always non-blocking (#31056)
When a synchronous update suspends, and we prerender the siblings, the
prerendering should be non-blocking so that we can immediately restart
once the data arrives.

This happens automatically when there's a Suspense boundary, because we
immediately commit the boundary and then proceed to a Retry render,
which are always concurrent. When there's not a Suspense boundary, there
is no Retry, so we need to take care to switch from the synchronous work
loop to the concurrent one, to enable time slicing.

DiffTrain build for commit https://github.com/facebook/react/commit/0f1856c49febe96923e469f98c0b123130ea015c.
2024-09-25 13:40:14 -07:00
acdlite cae834ff8c Unify perform{Sync,Concurrent}WorkOnRoot implementation (#31029)
Over time the behavior of these two paths has converged to be
essentially the same. So this merges them back into one function. This
should save some code size and also make it harder for the behavior to
accidentally diverge. (For the same reason, rolling out this change
might expose some areas where we had already accidentally diverged.)

DiffTrain build for commit https://github.com/facebook/react/commit/3c7667a694face1827356a7c90ee6f86a9c0baa0.
2024-09-25 12:41:53 -07:00
jackpope 84c55046e6 Increase nested update limit to 100 (#31061)
We're seeing the limit hit in some tests after enabling sibling
prerendering. Let's bump the limit so we can run more tests and gather
more signal on the changes. When we understand the scope of the problem
we can determine whether we need to change how the updates are counted
in prerenders and/or fix specific areas of product code.

DiffTrain build for commit https://github.com/facebook/react/commit/f9ebd85a196948be17efdd6774b4d0464b3b1f53.
2024-09-25 08:59:33 -07:00
poteto 4b77c87461 DiffTrain build for commit https://github.com/facebook/react/commit/. 2024-09-24 09:00:00 -07:00
Tim Yung c2c2363b4d Move react-dom/{ => cjs/}*.js 2024-09-23 16:56:38 -07:00
sebmarkbage 1aa95c1c69 [Fiber] Log the Render/Commit phases and the gaps in between (#31016)
A slight behavior change here too is that I now mark the start of the
commit phase before the BeforeMutationEffect phase. This affects
`<Profiler>` too.

The named sequences are as follows:

Render -> Suspended or Throttled -> Commit -> Waiting for Paint ->
Remaining Effects

The Suspended phase is only logged if we delay the Commit due to CSS /
images.

The Throttled phase is only logged if we delay the commit due to the
Suspense throttling timer.

<img width="1246" alt="Screenshot 2024-09-20 at 9 14 23 PM"
src="https://github.com/user-attachments/assets/8d01f444-bb85-472b-9b42-6157d92c81b4">

I don't yet log render phases that don't complete. I think I also need
to special case renders that or don't commit after being suspended.

DiffTrain build for commit https://github.com/facebook/react/commit/4e9540e3c2a8f9ae56318b967939c99b3a815190.
2024-09-23 11:17:19 -07:00
sebmarkbage 7afe7c4a5c [Fiber] Track Event Time, startTransition Time and setState Time (#31008)
This tracks the current window.event.timeStamp the first time we
setState or call startTransition. For either the blocking track or
transition track. We can use this to show how long we were blocked by
other events or overhead from when the user interacted until we got
called into React.

Then we track the time we start awaiting a Promise returned from
startTransition. We can use this track how long we waited on an Action
to complete before setState was called.

Then finally we track when setState was called so we can track how long
we were blocked by other word before we could actually start rendering.
For a Transition this might be blocked by Blocking React render work.

We only log these once a subsequent render actually happened. If no
render was actually scheduled, then we don't log these. E.g. if an
isomorphic Action doesn't call startTransition there's no render so we
don't log it.

We only log the first event/update/transition even if multiple are
batched into it later. If multiple Actions are entangled they're all
treated as one until an update happens. If no update happens and all
entangled actions finish, we clear the transition so that the next time
a new sequence starts we can log it.

We also clamp these (start the track later) if they were scheduled
within a render/commit. Since we share a single track we don't want to
create overlapping tracks.

The purpose of this is not to show every event/action that happens but
to show a prelude to how long we were blocked before a render started.
So you can follow the first event to commit.

<img width="674" alt="Screenshot 2024-09-20 at 1 59 58 AM"
src="https://github.com/user-attachments/assets/151ba9e8-6b3c-4fa1-9f8d-e3602745eeb7">

I still need to add the rendering/suspended phases to the timeline which
why this screenshot has a gap.

<img width="993" alt="Screenshot 2024-09-20 at 12 50 27 AM"
src="https://github.com/user-attachments/assets/155b6675-b78a-4a22-a32b-212c15051074">

In this case it's a Form Action which started a render into the form
which then suspended on the action. The action then caused a refresh,
which interrupts with its own update that's blocked before rendering.
Suspended roots like this is interesting because we could in theory
start working on a different root in the meantime which makes this
timeline less linear.

DiffTrain build for commit https://github.com/facebook/react/commit/d4688dfaafe51a4cb6e3c51fc2330662cb4e2296.
2024-09-20 11:34:48 -07:00
jackpope 45b2fc15d7 Update react-native/react-dom build directory (#31006)
Commit artifact actions are breaking after
https://github.com/facebook/react/pull/30711

See:
https://github.com/facebook/react/actions/runs/10930658977/job/30344033974

> mv: cannot stat 'build/facebook-react-native/react/dom/': No such file
or directory

After build, the new artifacts are in `/react-dom/cjs`, not
`/react/dom/`
```
$> yarn build
$> ls build/facebook-react-native/react/
# ... no dom
$> ls build/facebook-react-native/react-dom/cjs
```

DiffTrain build for commit https://github.com/facebook/react/commit/e4953922a99b5477c3bcf98cdaa2b13ac0a81f0d.
2024-09-19 15:11:59 -07:00
sebmarkbage 304fbaee2b Don't read currentTransition back from internals (#30991)
This code is weird. It reads back the transition that it just set from
the shared internals. It's almost like it expects it to be a getter or
something.

This avoids that and makes it consistent with what ReactFiberHooks
already does.

DiffTrain build for commit https://github.com/facebook/react/commit/15da9174518f18f82869767ebe2a21be2fc8bd90.
2024-09-17 12:32:15 -07:00
sebmarkbage f8e3da903b [Fiber] Optimize enableProfilerCommitHooks by Collecting Elapsed Effect Duration in Module Scope (#30981)
Stacked on #30979.

The problem with the previous approach is that it recursively walked the
tree up to propagate the resulting time from recording a layout effect.

Instead, we keep a running count of the effect duration on the module
scope. Then we reset it when entering a nested Profiler and then we add
its elapsed count when we exit the Profiler.

This also fixes a bug where we weren't previously including unmount
times for some detached trees since they couldn't bubble up to find the
profiler.

DiffTrain build for commit https://github.com/facebook/react/commit/4549be0f846e7df5a4eaabf06369d93bd120271e.
2024-09-17 12:19:37 -07:00
sebmarkbage 52878f5c6b Remove execution context check from shouldProfile (#30971)
I don't know why this is here since all these callsites are within the
CommitWork/CommitEffects helpers.

This should help with inlining.

DiffTrain build for commit https://github.com/facebook/react/commit/8152e5cd27721e792f395c0b62c8a7769a54777a.
2024-09-16 12:07:14 -07:00
javache 4c48f1fc24 [react-native] Fix misleading crash when view config is not found (#30970)
## Summary

When a view config can not be found, it currently errors with
`TypeError: Cannot read property 'bubblingEventTypes' of null`. Instead
invariant at the correct location and prevent further processing of the
null viewConfig to improve the error logged.

## How did you test this change?

Build and run RN playground app referencing an invalid native view
through `requireNativeComponent`.

DiffTrain build for commit https://github.com/facebook/react/commit/26855e4680dedb21f2c73a069ed691822a242db1.
2024-09-16 09:58:00 -07:00
sebmarkbage 4c73996cfd [Fiber] Log Component Renders to Custom Performance Track (#30967)
Stacked on #30960 and #30966. Behind the enableComponentPerformanceTrack
flag.

This is the first step of performance logging. This logs the start and
end time of a component render in the passive effect phase. We use the
data we're already tracking on components when the Profiler component or
DevTools is active in the Profiling or Dev builds. By backdating this
after committing we avoid adding more overhead in the hot path. By only
logging things that actually committed, we avoid the costly unwinding of
an interrupted render which was hard to maintain in earlier versions.

We already have the start time but we don't have the end time. That's
because `actualStartTime + actualDuration` isn't enough since
`actualDuration` counts the actual CPU time excluding yields and
suspending in the render.

Instead, we infer the end time to be the start time of the next sibling
or the complete time of the whole root if there are no more siblings. We
need to pass this down the passive effect tree. This will mean that any
overhead and yields are attributed to this component's span. In a follow
up, we'll need to start logging these yields to make it clear that this
is not part of the component's self-time.

In follow ups, I'll do the same for commit phases. We'll also need to
log more information about the phases in the top track. We'll also need
to filter out more components from the trees that we don't need to
highlight like the internal Offscreen components. It also needs polish
on colors etc.

Currently, I place the components into separate tracks depending on
which lane currently committed. That way you can see what was blocking
Transitions or Suspense etc. One problem that I've hit with the new
performance.measure extensions is that these tracks show up in the order
they're used which is not the order of priority that we use. Even when
you add fake markers they have to actually be within the performance run
since otherwise the calls are noops so it's not enough to do that once.

However, I think this visualization is actually not good because these
trees end up so large that you can't see any other lanes once you expand
one. Therefore, I think in a follow up I'll actually instead switch to a
model where Components is a single track regardless of lane since we
don't currently have overlap anyway. Then the description about what is
actually rendering can be separate lanes.

<img width="1512" alt="Screenshot 2024-09-15 at 10 55 55 PM"
src="https://github.com/user-attachments/assets/5ca3fa74-97ce-40c7-97f7-80c1dd7d6470">

<img width="1512" alt="Screenshot 2024-09-15 at 10 56 27 PM"
src="https://github.com/user-attachments/assets/557ad65b-4190-465f-843c-0bc6cbb9326d">

DiffTrain build for commit https://github.com/facebook/react/commit/f2df5694f2be141954f22618fd3ad035203241a3.
2024-09-16 08:53:02 -07:00
sebmarkbage a2e2949b98 [Fiber] Move Profiler onPostCommit processing of passive effect durations to plain passive effect (#30966)
We used to queue a separate third passive phase to invoke onPostCommit
but this is unnecessary. We can just treat it as a plain passive effect.
This means it is interleaved with other passive effects but we only need
to know the duration of the things below us which is already done at
this point.

I also extracted the user space call to onPostCommit into
ReactCommitEffects. Same as onCommit. It's now covered by
runWithFiberInDEV and catches.

DiffTrain build for commit https://github.com/facebook/react/commit/ee1a403a3019dd8bffb12174d269d8c85bfab8a1.
2024-09-16 08:17:45 -07:00
sebmarkbage baaa97f36b [Fiber] Profiler - Use two separate functions instead of branch by flag (#30957)
Nit: I don't trust flags in hot code. While it can take somewhat longer
to compile two functions and JIT them. After that they don't need to
check branches. Also makes it clearer the purpose.

DiffTrain build for commit https://github.com/facebook/react/commit/3d95c43b8967d4dda1ec9a22f0d9ea4999fee8b8.
2024-09-13 18:59:00 -07:00
rickhanlonii b42180d84a Call cleanup of insertion effects when hidden (#30954)
Insertion effects do not unmount when a subtree is removed while
offscreen.

Current behavior for an insertion effect is if the component goes

- *visible -> removed:* calls insertion effect cleanup
- *visible -> offscreen -> removed:* insertion effect cleanup is never
called

This makes it so we always call insertion effect cleanup when removing
the component.

Likely also fixes https://github.com/facebook/react/issues/26670

---------

Co-authored-by: Rick Hanlon <rickhanlonii@fb.com>

DiffTrain build for commit https://github.com/facebook/react/commit/d3d4d3a46b014ab0f6edc443c19fcdba09105f20.
2024-09-13 13:25:38 -07:00
sebmarkbage 03205d9bd9 [Fiber] Set profiler values to doubles (#30942)
At some point this trick was added to initialize the value first to NaN
and then replace them with zeros and negative ones.

This is to address the issue noted in
https://github.com/facebook/react/issues/14365 where these hidden
classes can be initialized to SMIs at first and then deopt when we
realize they're actually doubles.

However, this fix has been long broken and has deopted the profiling
build for years because closure compiler optimizes out the first write.

I'm not sure because I haven't A/B-tested this in the JIT yet but I
think we can use negative zero and -1.1 as the initial values instead
since they're not simple integers. Negative zero `===` zero (but not
Object.is) so is the same as far as our code is concerned. The negative
value is just `< 0` comparisons.

DiffTrain build for commit https://github.com/facebook/react/commit/94e4acaa1477e65cac02ba86058cde0afe4c8f1f.
2024-09-13 09:33:56 -07:00
acdlite f9e2396524 Start prerendering Suspense retries immediately (#30934)
When a component suspends and is replaced by a fallback, we should start
prerendering the fallback immediately, even before any new data is
received. During the retry, we can enter prerender mode directly if
we're sure that no new data was received since we last attempted to
render the boundary.

To do this, when completing the fallback, we leave behind a pending
retry lane on the Suspense boundary. Previously we only did this once a
promise resolved, but by assigning a lane during the complete phase, we
will know that there's speculative work to be done.

Then, upon committing the fallback, we mark the retry lane as suspended
— but only if nothing was pinged or updated in the meantime. That allows
us to immediately enter prerender mode (i.e. render without skipping any
siblings) when performing the retry.

DiffTrain build for commit https://github.com/facebook/react/commit/d6cb4e771341ff82489c00f4907990cb8a75696b.
2024-09-11 08:49:11 -07:00
sebmarkbage 9b5d5c32af [Fiber] Don't call performance.now() twice in a row (#30936)
DiffTrain build for commit https://github.com/facebook/react/commit/a5a7f1063e6f190c64c779b2aa54d28fb7b92cfb.
2024-09-10 13:54:16 -07:00
acdlite c56b0edd51 Prerender during same pass if blocked anyway (#30879)
If something suspends in the shell — i.e. we won't replace the suspended
content with a fallback — we might as well prerender the siblings during
the current render pass, instead of spawning a separate prerender pass.

This is implemented by setting the "is prerendering" flag to true
whenever we suspend in the shell. But only if we haven't already skipped
over some siblings, because if so, then we need to schedule a separate
prerender pass regardless.

DiffTrain build for commit https://github.com/facebook/react/commit/66cf2cfc8a8c4b09d2b783fd7302ae6b24150935.
2024-09-10 10:30:51 -07:00
SamChou19815 12971be8e9 [flow] Upgrade Flow to 0.245.2 (#30919)
## Summary

This PR bumps Flow all the way to the latest 0.245.2.

Most of the suppressions comes from Flow v0.239.0's change to include
undefined in the return of `Array.pop`.

I also enabled `react.custom_jsx_typing=true` and added custom jsx
typing to match the old behavior that `React.createElement` is
effectively any typed. This is necessary since various builtin
components like `React.Fragment` is actually symbol in the React repo
instead of `React.AbstractComponent<...>`. It can be made more accurate
by customizing the `React$CustomJSXFactory` type, but I will leave it to
the React team to decide.

## How did you test this change?

`yarn flow` for all the renderers

DiffTrain build for commit https://github.com/facebook/react/commit/e210d08180a63f42079b91acaa7f6af15eef6d32.
2024-09-09 08:51:21 -07:00
josephsavona 15892717ea Fix useMemoCache with setState in render
Fixes the bug that @alexmckenley and @mofeiZ found where setState-in-render can reset useMemoCache and cause an infinite loop. The bug was that renderWithHooksAgain() was not resetting hook state when rerendering (so useMemo values were preserved) but was resetting the updateQueue. This meant that the entire memo cache was cleared on a setState-in-render.

The fix here is to call a new helper function to clear the update queue. It nulls out other properties, but for memoCache it just sets the index back to zero.

ghstack-source-id: fc0947ce21
Pull Request resolved: https://github.com/facebook/react/pull/30889

DiffTrain build for commit https://github.com/facebook/react/commit/727b3615287074ddaa28069bfbd2dfee8cf73575.
2024-09-06 14:40:39 -07:00
sebmarkbage 6f1fca9c4f [Fiber] Move runWithFiberInDEV from CommitWork to CommitEffects (#30882)
Stacked on #30881.

Move `runWithFiberInDEV` from the recursive part of the commit phase and
instead wrap each call into user space. These should really map 1:1 with
where we're using `try/catch` since that's where we're calling into user
space.

The goal of this is to avoid the extra stack frames added by
`enableOwnerStacks` in the recursive parts to avoid stack overflow. This
way we only have a couple of extra at the end of the stack instead of a
couple of extra at every depth of the tree.

DiffTrain build for commit https://github.com/facebook/react/commit/a03254bc60b06c535c37e43c53b1fd40757b2ef4.
2024-09-05 18:04:03 -07:00
sebmarkbage d118acbb84 [Fiber] Extract Functions that Call User Space and Host Configs in Commit to Separate Modules (#30881)
This is mostly just moves and same code extracted into utility
functions.

This is to help clarify what needs to be wrapped in try/catch and
runWithFiberInDEV. I'll do the runWithFiberInDEV changes in a follow up.

This leaves ReactCommitWork mostly to do matching on the tag and the
recursive loops.

DiffTrain build for commit https://github.com/facebook/react/commit/fe03c56d1e51379a18676b04cf185e76f04cd457.
2024-09-05 18:02:01 -07:00
acdlite 9e99471041 Schedule prerender after something suspends (#30800)
Adds the concept of a "prerender". These special renders are spawned
whenever something suspends (and we're not already prerendering).

The purpose is to move speculative rendering work into a separate phase
that does not block the UI from updating. For example, during a
transition, if something suspends, we should not speculatively prerender
siblings that will be replaced by a fallback in the UI until *after* the
fallback has been shown to the user.

DiffTrain build for commit https://github.com/facebook/react/commit/e10e8681824e56c10fdb14e0359d878bcd748937.
2024-09-04 11:04:03 -07:00
acdlite 9625df2bf8 Temporarily disable suspending during work loop (#30762)
### Based on

- #30761
- #30759

---

`use` has an optimization where in some cases it can suspend the work
loop during the render phase until the data has resolved, rather than
unwind the stack and lose context. However, the current implementation
is not compatible with sibling prerendering. So I've temporarily
disabled it until the sibling prerendering has been refactored. We will
add it back in a later step.

DiffTrain build for commit https://github.com/facebook/react/commit/8b4c54c00f5c047a72a4cecc2689196786c3e5ff.
2024-09-04 09:47:08 -07:00
sebmarkbage 18e907e144 [Fiber] Stash ThenableState on the Dependencies Object for Use By DevTools (#30866)
This lets us track what a Component might suspend on from DevTools. We
could already collect this by replaying a component's Hooks but that
would be expensive to collect from a whole tree.

The thenables themselves might contain useful information but mainly
we'd want access to the `_debugInfo` on the thenables which might
contain additional information from the server.

https://github.com/facebook/react/blob/19bd26beb689e554fceb0b929dc5199be8cba594/packages/shared/ReactTypes.js#L114

In a follow up we should really do something similar in Flight to
transfer `use()` on the debugInfo of that Server Component.

DiffTrain build for commit https://github.com/facebook/react/commit/8d68da3f7396064614f34b84881fe8833b6039ac.
2024-09-03 13:12:53 -07:00
rickhanlonii 3ba4b124b2 Enable suspenseCallback on React Native (#29210)
## Summary

suspenseCallback feature has proved to be useful for FB Web. Let's look
at enabling the feature for the React Native build.

## How did you test this change?

Will sync the react changes with a React Native build and will verify
that performance logging is correctly notified of suspense promises
during the suspense callback.

DiffTrain build for commit https://github.com/facebook/react/commit/246d7bfeb0c90ecccd9531929b60a79d628a4c78.
2024-08-26 19:14:20 -07:00
acdlite 6c99ba76c9 Fix: Synchronous popstate transitions (#30759)
This is a refactor of the fix in #27505.

When a transition update is scheduled by a popstate event, (i.e. a back/
forward navigation) we attempt to render it synchronously even though
it's a transition, since it's likely the previous page's data is cached.

In #27505, I changed the implementation so that it only "upgrades" the
priority of the transition for a single attempt. If the attempt
suspends, say because the data is not cached after all, from then on it
should be treated as a normal transition.

But it turns out #27505 did not work as intended, because it relied on
marking the root with pending synchronous work (root.pendingLanes),
which was never cleared until the popstate update completed.

The test scenarios I wrote accidentally worked for a different reason
related to suspending the work loop, which I'm currently in the middle
of refactoring.

DiffTrain build for commit https://github.com/facebook/react/commit/ee7f6757c446c4e79ecc7e2bc22b8c9b712834b7.
2024-08-23 09:38:02 -07:00
SamChou19815 99d43a14d0 [flow] Eliminate a few React.Element type that will be synced to react-native (#30719)
## Summary

Flow will eventually remove the specific `React.Element` type. For most
of the code, it can be replaced with `React.MixedElement` or
`React.Node`.

When specific react elements are required, it needs to be replaced with
either `React$Element` which will trigger a `internal-type` lint error
that can be disabled project-wide, or use
`ExactReactElement_DEPRECATED`.

Fortunately in this case, this one can be replaced with just
`React.MixedElement`.

## How did you test this change?

`flow`

DiffTrain build for commit https://github.com/facebook/react/commit/85fb95cdffdd95f2f908ee71974cae06b1c866e1.
2024-08-16 10:03:44 -07:00
gaearon 220e25c8db [Fresh] Always reset useMemoCache on Fast Refresh (#30700)
Stacked on https://github.com/facebook/react/pull/30662.

Alternative to https://github.com/facebook/react/pull/30663 and
https://github.com/facebook/react/pull/30677.

During a Fast Refresh, we always want to evict the memo cache, same as
we do with normal `useMemo`. The mechanism used by `useMemo` and other
Hooks is this module-level variable:

https://github.com/facebook/react/blob/fca5d655d78917400a2722287351c20938166669/packages/react-reconciler/src/ReactFiberHooks.js#L304-L307

which has DEV-only behavior as if the dependencies are always different:

https://github.com/facebook/react/blob/fca5d655d78917400a2722287351c20938166669/packages/react-reconciler/src/ReactFiberHooks.js#L451-L460

The `useMemoCache` Hook doesn't use a dependency array but conceptually
I think we want the same behavior.

## Test Plan

The test passes.

---------

Co-authored-by: Lauren Tan <poteto@users.noreply.github.com>

DiffTrain build for commit https://github.com/facebook/react/commit/7e8a06cf4c628be45171da52c1a8e97f9869b7ee.
2024-08-14 17:11:17 -07:00
kassens 7a7933bb8c Cloned flag to avoid extra clones in persistent renderer (#27647)
Persistent renderers used the `Update` effect flag to check if a subtree
needs to be cloned. In some cases, that causes extra renders, such as
when a layout effect is triggered which only has an effect on the JS
side, but doesn't update the host components.

It's been a bit tricky to find the right places where this needs to be
set and I'm not 100% sure I got all the cases even though the tests
passed.

DiffTrain build for commit https://github.com/facebook/react/commit/5fb67fa25c4ea8be046c6d9af41047f3cc379279.
2024-08-01 12:18:26 -07:00