Commit Graph

1158 Commits

Author SHA1 Message Date
Brian Vaughn
895ae67fd3 Improve error boundary handling for unmounted subtrees (#20645)
A passive effect's cleanup function may throw after an unmount. Prior to this commit, such an error would be ignored. (React would not notify any error boundaries.)

After this commit, React will skip any unmounted boundaries and look for a still-mounted boundary. If one is found, it will call getDerivedStateFromError and/or componentDidCatch (depending on the type of boundary). Unmounted boundaries will be ignored, but as they have been unmounted– this seems appropriate.
2021-01-25 08:54:20 -05:00
Andrew Clark
af0bb68e87 Land #20595 and #20596 in main fork (#20602) 2021-01-19 08:37:51 -08:00
Andrew Clark
efc57e5cbb Add built-in Suspense cache with support for invalidation (refreshing) (#20456) 2020-12-18 10:57:24 -08:00
inokawa
cdae31ab8e Fix typo (#20279) 2020-12-08 10:40:35 -05:00
Andrew Clark
88ef95712d Fork ReactFiberLane (#20371)
This wasn't forked previously because Lane and associated types are
opaque, and they leak into non-reconciler packages. So forking the type
would also require forking all those other packages.

But I really want to use the reconciler fork infra for lanes changes.
So I made them no longer opaque.

Another possible solution would be to add separate `new` and `old`
fields to the Fiber type, like I did when migrating from expiration
times. But that seems so excessive. This seems fine.

But we should still treat them like they're opaque and only do lanes
manipulation in the ReactFiberLane module. At least until the model
stabilizes more. We'll just need to enforce this with discipline
instead of with the type system.
2020-12-04 10:54:09 -06:00
Dan Abramov
e23673b511 [Flight] Add getCacheForType() to the dispatcher (#20315)
* Remove react/unstable_cache

We're probably going to make it available via the dispatcher. Let's remove this for now.

* Add readContext() to the dispatcher

On the server, it will be per-request.

On the client, there will be some way to shadow it.

For now, I provide it on the server, and throw on the client.

* Use readContext() from react-fetch

This makes it work on the server (but not on the client until we implement it there.)

Updated the test to use Server Components. Now it passes.

* Fixture: Add fetch from a Server Component

* readCache -> getCacheForType<T>

* Add React.unstable_getCacheForType

* Add a feature flag

* Fix Flow

* Add react-suspense-test-utils and port tests

* Remove extra Map lookup

* Unroll async/await because build system

* Add some error coverage and retry

* Add unstable_getCacheForType to Flight entry
2020-12-03 03:44:56 +00:00
Brian Vaughn
a2a025537d Fixed invalid DevTools work tags (#20362)
* Fixed invalid DevTools work tags

Work tags changed recently (PR #13902) but we didn't bump React versions. This meant that DevTools has valid work tags only for master (and FB www sync) but invalid work tags for the latest open source releases. To fix this, I incremneted React's version in Git (without an actual release) and added a new fork to the work tags detection branch.

This commit also adds tags for the experimental Scope and Fundamental APIs to DevTools so component names will at least display correctly. Technically these new APIs were first introduced to experimental builds ~16.9 but I didn't add a new branch to the work tags fork because I don't they're used commonly. I've just added them to the 17+ branches.

* Removed FundamentalComponent from DevTools tag defs
2020-12-01 10:33:32 -05:00
Andrew Clark
ac2cff4b10 Warn if commit phase error thrown in detached tree (#20286)
Until `skipUnmountedBoundaries` lands again, we need some way to detect
when errors are thrown inside a deleted tree. I've added a warning to
`captureCommitPhaseError` that fires when we reach the root of a subtree
without finding either a boundary or a HostRoot.

Even after `skipUnmountedBoundaries`  lands, this warning could be a
useful guard against internal bugs, like a bug in the
`skipUnmountedBoundaries` implementation itself.

In the meantime, do not add this warning to the allowlist; this is only
for our internal use. For this reason, I've also only added it to the
new fork, not the old one, to prevent this from accidentally leaking
into the open source build.
2020-11-18 09:23:39 -08:00
Andrew Clark
765e89b908 Reset new fork to old fork (#20254)
* Fix typo

This typo was fixed in the new fork but not the old.

* Reset new fork to old fork

Something in the new fork is causing a topline metrics regression. We're
not sure what it is, so we're going to split it into steps and bisect.

As a first step, this resets the new fork back to the contents of the
old fork. We will land this to confirm that the fork infra itself is
not causing a regression.

* Fix tests: Add `dfsEffectsRefactor` flag

Some of the tests that gated on the effects refactor used the `new`
flag. In order to bisect, we'll need to decompose the new fork changes
into multiple steps.

So I added a hardcoded test flag called `dfsEffectsRefactor` and set it
to false. Will turn back on when we switch back to traversing the
finished tree using DFS and `subtreeTag`.
2020-11-13 11:54:33 -08:00
Andrew Clark
15df051c94 Add warning if return pointer is inconsistent (#20219)
Bugs caused by inconsistent return pointers are tricky to diagnose
because the source of the error is often in a different part of the
codebase from the actual mistake. For example, you might forget to set a
return pointer during the render phase, which later causes a crash in
the commit phase.

This adds a dev-only invariant to the commit phase to check for
inconsistencies. With this in place, we'll hopefully catch return
pointer errors quickly during local development, when we have the most
context for what might have caused it.
2020-11-11 09:06:37 -06:00
Brian Vaughn
7a73d6a0f9 (Temporarily) revert unmounting error boundaries changes (#20147)
This reverts commits bcca5a6ca7 and ffb749c95e, although neither revert cleanly since methods have been moved between the work-loop and commit-work files. This commit is a mostly manual effort of undoing the changes.
2020-11-09 10:14:24 -05:00
inokawa
fc06cf8d35 Fix typo (#20188) 2020-11-09 09:17:40 -05:00
Deniz Susman
31d096605c typo fix in comment (#20153) 2020-11-03 09:57:56 -05:00
Ricky
454c2211c0 Refactor SchedulerHostConfigs (#20025)
* Remove SchedulerHostConfigs

* Fix builds

* Fix forks

* Move SchedulerNoDom check to npm/index.js

* Fix tests

* Add @gate source

* Gate build-only test to build test runs
2020-11-02 12:46:58 -05:00
Dan Abramov
eaaf4cbce7 17.0.1 2020-10-22 13:24:46 +01:00
Dan Abramov
f021a983aa Bump versions for 17 (#20062) 2020-10-20 21:41:18 +01:00
Brian Vaughn
c59c3dfe55 useRef: Warn about reading or writing mutable values during render (#18545)
Reading or writing a ref value during render is only safe if you are implementing the lazy initialization pattern.

Other types of reading are unsafe as the ref is a mutable source.

Other types of writing are unsafe as they are effectively side effects.

This change also refactors useTransition to no longer use a ref hook, but instead manage its own (stable) hook state.
2020-10-19 16:05:00 -04:00
Dan Abramov
e9f5ad2584 Remove Set bookkeeping for root events (#19990)
* Remove dead code branch

This function is only called when initializing roots/containers (where we skip non-delegated events) and in the createEventHandle path for non-DOM nodes (where we never hit this path because targetElement is null).

* Move related functions close to each other

* Fork listenToNativeEvent for createEventHandle

It doesn't need all of the logic that's needed for normal event path.

And the normal codepath doesn't use the last two arguments.

* Expand test coverage for non-delegated events

This changes a test to fail if we removed the event handler Sets. Previously, we didn't cover that.

* Add DEV-level check that top-level events and non-delegated events do not overlap

This makes us confident that they're mutually exclusive and there is no duplication between them.

* Add a test verifying selectionchange deduplication

This is why we still need the Set bookkeeping. Adding a test for it.

* Remove Set bookkeeping for root events

Root events don't intersect with non-delegated bubbled events (so no need to deduplicate there). They also don't intersect with createEventHandle non-managed events (because those don't go on the DOM elements). So we can remove the bookeeping because we already have code ensuring the eager subscriptions only run once per element.

I've moved the selectionchange special case outside, and added document-level deduplication for it alone.

Technically this might change the behavior of createEventHandle with selectionchange on the document, but we're not using that, and I'm not sure that behavior makes sense anyway.

* Flow
2020-10-16 16:49:41 +01:00
Sebastian Silbermann
b093528650 test: Add regression test for hooks after error boundaries (#20002)
* test: Add regression test for hooks after error boundaries

* fix lint
2020-10-16 16:07:27 +01:00
Dominic Gannaway
d95c4938df [EventSystem] Revise onBeforeBlur propagation mechanics (#20020) 2020-10-14 23:38:57 +01:00
Dan Abramov
993ca533b4 Enable eager listeners statically (#19983) 2020-10-08 19:32:28 +01:00
Shivam Sandbhor
4ead6b5305 Treat <time> tag as a normal HTML tag. (#19951)
<time> tag has been supported by Chrome since Chrome 62.0.
Remove workarounds which were in place to avoid friction with
versions before Chrome 62.

Signed-off-by: Shivam Sandbhor <shivam.sandbhor@gmail.com>
2020-10-06 03:15:32 +01:00
Dan Abramov
480626a9e9 Create Synthetic Events Lazily (#19909) 2020-09-25 13:33:28 +01:00
Dan Abramov
0a00804494 Remove Array.from() from hot path (#19908)
* Remove Array.from() from hot path

* Fix build

Don't declare block variables inside loops
2020-09-25 11:33:07 +01:00
Dan Abramov
1890159a5d Separate SyntheticEvent constructors to prevent deopts (#19907)
* Remove arguments from hot path

* Make SyntheticEvent subtypes monomorphic

* Maybe fix Flow?
2020-09-25 11:31:00 +01:00
Dan Abramov
6fddca27e7 Remove passive intervention flag (#19849) 2020-09-17 15:37:12 +01:00
Andrew Clark
b93f3e7d2d Fix act bundle size regression (#19832)
Adds back the `TestUtils.act` implementation that I had removed
in #19745. This version of `act` is implemented in "userspace" (i.e. not
the reconciler), so it doesn't add to the production bundle size.

I had removed this in #19745 in favor of the `act` exported by the
reconciler because I thought we would remove support for `act` in
production in the impending major release. (It currently warns.)

However, we've since decided to continue supporting `act` in prod for
now, so that it doesn't block people from upgrading to v17. We'll drop
support in a future major release.

So, to avoid bloating the production bundle size, we need to move the
public version of `act` back to "userspace", like it was before.

This doesn't negate the main goal of #19745, though, which was to
decouple the public version(s) of `act` from the internal one that we
use to test React itself.
2020-09-14 10:11:47 -07:00
Dan Abramov
11ee82df45 [Events] Make passiveness and priority non-configurable (#19807) 2020-09-14 13:54:08 +01:00
Dan Abramov
a08ae9f147 Listen to onScroll during hydration (#19803) 2020-09-10 11:00:02 +01:00
Andrew Clark
e7b255341b Internal act: Flush timers at end of scope (#19788)
If there are any suspended fallbacks at the end of the `act` scope,
force them to display by running the pending timers (i.e. `setTimeout`).

The public implementation of `act` achieves the same behavior with an
extra check in the work loop (`shouldForceFlushFallbacks`). Since our
internal `act` needs to work in both development and production, without
additional runtime checks, we instead rely on Jest's mock timers.

This doesn't not affect refresh transitions, which are meant to delay
indefinitely, because in that case we exit the work loop without
posting a timer.
2020-09-08 21:55:23 -07:00
Andrew Clark
d17086c7c8 Decouple public, internal act implementation (#19745)
In the next major release, we intend to drop support for using the `act`
testing helper in production. (It already fires a warning.) The
rationale is that, in order for `act` to work, you must either mock the
testing environment or add extra logic at runtime. Mocking the testing
environment isn't ideal because it requires extra set up for the user.
Extra logic at runtime is fine only in development mode — we don't want
to slow down the production builds.

Since most people only run their tests in development mode, dropping
support for production should be fine; if there's demand, we can add it
back later using a special testing build that is identical to the
production build except for the additional testing logic.

One blocker for removing production support is that we currently use
`act` to test React itself. We must test React in both development and
production modes.

So, the solution is to fork `act` into separate public and
internal implementations:

- *public implementation of `act`* – exposed to users, only works in
  development mode, uses special runtime logic, does not support partial
  rendering
- *internal implementation of `act`* – private, works in both
  development and productionm modes, only used by the React Core test
  suite, uses no special runtime logic, supports partial rendering (i.e.
  `toFlushAndYieldThrough`)

The internal implementation should mostly match the public
implementation's behavior, but since it's a private API, it doesn't have
to match exactly. It works by mocking the test environment: it uses a
mock build of Scheduler to flush rendering tasks, and Jest's mock timers
to flush Suspense placeholders.

---

In this first commit, I've added the internal forks of `act` and
migrated our tests to use them. The public `act` implementation is
unaffected for now; I will leave refactoring/clean-up for a later step.
2020-09-08 08:11:45 -07:00
Dan Abramov
bcc0aa4633 Revert "Revert "Remove onScroll bubbling flag (#19535)" (#19655)" (#19761)
This reverts commit 64ddef44c6.
2020-09-03 17:06:20 +01:00
Andrew Clark
1f38dcff67 Remove withSuspenseConfig (#19724)
Removes `withSuspenseConfig` and migrates relevant tests to
`startTransition` instead.

We only had one caller in www, which I've removed.
2020-08-31 09:16:49 -07:00
Andrew Clark
4f5fb56100 Use gate pragma instead of if (__EXPERIMENTAL__) (#19722)
* Use gate pragma instead of if (__EXPERIMENTAL__)

* Fix stream error handling in tests

Added an error listener so that the tests fail within their Jest scope,
instead of crashing the whole process.
2020-08-28 14:21:01 -07:00
Andrew Clark
ddd1faa197 Remove config argument from useTransition (#19719)
And `useDeferredValue`.

The options were already disabled in previous commits, so this doesn't
change any behavior. I upated type signatures and cleaned up the hook
implementation a bit — no longer have to wrap the `start` method with
`useCallback`, because its only remaining dependency is a `setState`
method, which never changes. Instead, we can store the `start` method
on a ref.
2020-08-28 11:49:01 -07:00
Andrew Clark
92fcd46cc7 Replace SuspenseConfig object with an integer (#19706)
Now that the options in SuspenseConfig are no longer supported, the
only thing we use it for is to track whether an update is part of
a transition.

I've renamed `ReactCurrentBatchConfig.suspense` to
`ReactCurrentBatchConfig.transition`, and changed the type to a number.
The number is always either 0 or 1. I could have made it a boolean;
however, most likely this will eventually be either a Lane or an
incrementing identifier.

The `withSuspenseConfig` export still exists until we've removed
all the callers from www.
2020-08-28 10:52:56 -07:00
Dan Abramov
b754caaaf2 Enable eager listeners in open source (#19716)
* Enable eager listeners in open source

* Fix tests

* Enable in all places
2020-08-28 12:23:28 +01:00
Andrew Clark
8da0da0937 Disable timeoutMs argument (#19703)
* Remove distinction between long, short transitions

We're removing the `timeoutMs` option, so there's no longer any
distinction between "short" and "long" transitions. They're all treated
the same.

This commit doesn't remove `timeoutMs` yet, only combines the internal
priority levels.

* Disable `timeoutMs` argument

tl;dr
-----

- We're removing the `timeoutMs` argument from `useTransition`.
- Transitions will either immediately switch to a skeleton/placeholder
  view (when loading new content) or wait indefinitely until the data
  resolves (when refreshing stale content).
- This commit disables the `timeoutMS` so that the API has the desired
  semantics. It doesn't yet update the types or migrate all the test
  callers. I'll do those steps in follow-up PRs.

Motivation
----------

Currently, transitions initiated by `startTransition` / `useTransition`
accept a `timeoutMs` option. You can use this to control the maximum
amount of time that a transition is allowed to delay before we give up
and show a placeholder.

What we've discovered is that, in practice, every transition falls into
one of two categories: a **load** or a **refresh**:

- **Loading a new screen**: show the next screen as soon as possible,
  even if the data hasn't finished loading. Use a skeleton/placeholder
  UI to show progress.
- **Refreshing a screen that's already visible**: keep showing the
  current screen indefinitely, for as long as it takes to load the fresh
  data, even if the current data is stale. Use a pending state (and
  maybe a busy indicator) to show progress.

In other words, transitions should either *delay indefinitely* (for a
refresh) or they should show a placeholder *instantly* (for a load).
There's not much use for transitions that are delayed for a
small-but-noticeable amount of time.

So, the plan is to remove the `timeoutMs` option. Instead, we'll assign
an effective timeout of `0` for loads, and `Infinity` for refreshes.

The mechanism for distinguishing a load from a refresh already exists in
the current model. If a component suspends, and the nearest Suspense
boundary hasn't already mounted, we treat that as a load, because
there's nothing on the screen. However, if the nearest boundary is
mounted, we treat that as a refresh, since it's already showing content.

If you need to fix a transition to be treated as a load instead of a
refresh, or vice versa, the solution will involve rearranging the
location of your Suspense boundaries. It may also involve adding a key.

We're still working on proper documentation for these patterns. In the
meantime, please reach out to us if you run into problems that you're
unsure how to fix.

We will remove `timeoutMs` from `useDeferredValue`, too, and apply the
same load versus refresh semantics to the update that spawns the
deferred value.

Note that there are other types of delays that are not related to
transitions; for example, we will still throttle the appearance of
nested placeholders (we refer to this as the placeholder "train model"),
and we may still apply a Just Noticeable Difference heuristic (JND) in
some cases. These aren't going anywhere. (Well, the JND heuristic might
but for different reasons than those discussed above.)
2020-08-26 14:35:13 -07:00
Dan Abramov
848bb2426e Attach Listeners Eagerly to Roots and Portal Containers (#19659)
* Failing test for #19608

* Attach Listeners Eagerly to Roots and Portal Containers

* Forbid createEventHandle with custom events

We can't support this without adding more complexity. It's not clear that this is even desirable, as none of our existing use cases need custom events. This API primarily exists as a deprecation strategy for Flare, so I don't think it is important to expand its support beyond what Flare replacement code currently needs. We can later revisit it with a better understanding of the eager/lazy tradeoff but for now let's remove the inconsistency.

* Reduce risk by changing condition only under the flag

Co-authored-by: koba04 <koba0004@gmail.com>
2020-08-24 16:50:20 +01:00
Dominic Gannaway
8c9fc4e90f Remove usage of PossiblyWeakSet from createEventHandle (#19686) 2020-08-24 16:10:23 +01:00
Toru Kobayashi
2ada4bd0c2 Add a test for non-passive event handlers for events affected by the browsers' intervention (#19658) 2020-08-24 14:30:17 +01:00
Dan Abramov
90d212d326 Fix movementX/Y polyfill with capture events (#19672)
* Fix movementX/Y polyfill with capture events

* Remove unnecesary call for better inlining
2020-08-21 16:55:41 +01:00
Hemakshi Sachdev
f912186012 Fix: Unspecified error for IE11 (#19530) (#19664) 2020-08-20 21:53:23 +01:00
Dan Abramov
08e69f65b4 Fix incorrect copy-paste in test (#19657) 2020-08-20 01:47:32 +01:00
Dan Abramov
64ddef44c6 Revert "Remove onScroll bubbling flag (#19535)" (#19655)
This reverts commit e9721e14e4.
2020-08-19 20:54:54 +01:00
Dan Abramov
dd651df05e Keep onTouchStart, onTouchMove, and onWheel passive (#19654)
* Keep onTouchStart, onTouchMove, and onWheel passive

* Put it behind a feature flag on WWW
2020-08-19 18:42:33 +01:00
Sebastian Silbermann
87b3e2d257 Add flow to SyntheticEvent (#19564)
* Add flow to SyntheticEvent

* Minimal implementation of known and unknown synthetic events

* less casting

* Update EnterLeaveEventPlugin.js

Co-authored-by: Dan Abramov <dan.abramov@gmail.com>
2020-08-19 14:16:48 +01:00
Toru Kobayashi
32ca313060 refactor: remove unnecessary return statements (#19625) 2020-08-19 14:10:41 +01:00
Brian Vaughn
bcca5a6ca7 Always skip unmounted/unmounting error boundaries (#19627)
The behavior of error boundaries for passive effects that throw during cleanup was recently changed so that React ignores boundaries which are also unmounting in favor of still-mounted boundaries. This commit implements that same behavior for layout effects (useLayoutEffect, componentWillUnmount, and ref-detachment).

The new, skip-unmounting-boundaries behavior is behind a feature flag (`skipUnmountedBoundaries`).
2020-08-17 15:01:06 -04:00
Jack Works
1287670191 Fix: React cannot render in ShadowRoot (#15894)
* fix: render in shadow root

* fix: flow typing

* Remove types and turn invariant into warning

Co-authored-by: Dan Abramov <dan.abramov@me.com>
2020-08-17 15:47:49 +01:00