Commit a0bb10a
committed
[Experiment] Context Selectors
For internal experimentation only.
This implements `unstable_useContextSelector` behind a feature flag.
It's based on [RFC 119](reactjs/rfcs#119) and
[RFC 118](reactjs/rfcs#118) by @gnoff.
Usage:
```js
const context = useContextSelector(Context, c => c.selectedField);
```
The key feature is that if the selected value does not change between
renders, the component will bail out of rendering its children, a la
`memo`, `PureComponent`, or the `useState` bailout mechanism. (Unless
some other state, props, or context was updated in the same render.)
One difference from the RFC is that it does not return the selected
value. It returns the full context object. This serves a few purposes:
it discourages you from creating any new objects or derived values
inside the selector, because it'll get thrown out regardless. Instead,
all the selector will do is return a subfield. Then you can compute
the derived value inside the component, and if needed, you memoize that
derived value with `useMemo`.
If all the selectors do is access a subfield, they're (theoretically)
fast enough that we can call them during the propagation scan and bail
out really early, without having to visit the component during the
render phase.
Another benefit is that it's API compatible with `useContext`. So we can
put it behind a flag that falls back to regular `useContext`.
The longer term vision is that these optimizations (in addition to other
memoization checks, like `useMemo` and `useCallback`) are inserted
automatically by a compiler. So you would write code like this:
```js
const {a, b} = useContext(Context);
const derived = computeDerived(a, b);
```
and it would get converted to something like this:
```js
const {a} = useContextSelector(Context, context => context.a);
const {b} = useContextSelector(Context, context => context.b);
const derived = useMemo(() => computeDerived(a, b), [a, b]);
```
(Though not this exactly. Some lower level compiler output target.)1 parent 9c7f29e commit a0bb10a
17 files changed
Lines changed: 518 additions & 15 deletions
File tree
- packages
- react-debug-tools/src
- react-dom/src/server
- react-reconciler/src
- __tests__
- react-server/src
- react-suspense-test-utils/src
- react
- src
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
123 | 123 | | |
124 | 124 | | |
125 | 125 | | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
126 | 138 | | |
127 | 139 | | |
128 | 140 | | |
| |||
316 | 328 | | |
317 | 329 | | |
318 | 330 | | |
| 331 | + | |
319 | 332 | | |
320 | 333 | | |
321 | 334 | | |
| |||
Lines changed: 14 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
245 | 245 | | |
246 | 246 | | |
247 | 247 | | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
248 | 261 | | |
249 | 262 | | |
250 | 263 | | |
| |||
497 | 510 | | |
498 | 511 | | |
499 | 512 | | |
| 513 | + | |
500 | 514 | | |
501 | 515 | | |
502 | 516 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
56 | 56 | | |
57 | 57 | | |
58 | 58 | | |
59 | | - | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
60 | 64 | | |
61 | 65 | | |
62 | 66 | | |
| |||
2067 | 2071 | | |
2068 | 2072 | | |
2069 | 2073 | | |
| 2074 | + | |
2070 | 2075 | | |
2071 | 2076 | | |
2072 | 2077 | | |
| |||
2092 | 2097 | | |
2093 | 2098 | | |
2094 | 2099 | | |
| 2100 | + | |
2095 | 2101 | | |
2096 | 2102 | | |
2097 | 2103 | | |
| |||
2117 | 2123 | | |
2118 | 2124 | | |
2119 | 2125 | | |
| 2126 | + | |
2120 | 2127 | | |
2121 | 2128 | | |
2122 | 2129 | | |
| |||
2142 | 2149 | | |
2143 | 2150 | | |
2144 | 2151 | | |
| 2152 | + | |
2145 | 2153 | | |
2146 | 2154 | | |
2147 | 2155 | | |
| |||
2204 | 2212 | | |
2205 | 2213 | | |
2206 | 2214 | | |
| 2215 | + | |
| 2216 | + | |
| 2217 | + | |
| 2218 | + | |
| 2219 | + | |
| 2220 | + | |
| 2221 | + | |
| 2222 | + | |
| 2223 | + | |
| 2224 | + | |
| 2225 | + | |
2207 | 2226 | | |
2208 | 2227 | | |
2209 | 2228 | | |
| |||
2332 | 2351 | | |
2333 | 2352 | | |
2334 | 2353 | | |
| 2354 | + | |
| 2355 | + | |
| 2356 | + | |
| 2357 | + | |
| 2358 | + | |
| 2359 | + | |
| 2360 | + | |
| 2361 | + | |
| 2362 | + | |
| 2363 | + | |
| 2364 | + | |
2335 | 2365 | | |
2336 | 2366 | | |
2337 | 2367 | | |
| |||
2456 | 2486 | | |
2457 | 2487 | | |
2458 | 2488 | | |
| 2489 | + | |
| 2490 | + | |
| 2491 | + | |
| 2492 | + | |
| 2493 | + | |
| 2494 | + | |
| 2495 | + | |
| 2496 | + | |
| 2497 | + | |
| 2498 | + | |
| 2499 | + | |
2459 | 2500 | | |
2460 | 2501 | | |
2461 | 2502 | | |
| |||
2581 | 2622 | | |
2582 | 2623 | | |
2583 | 2624 | | |
| 2625 | + | |
| 2626 | + | |
| 2627 | + | |
| 2628 | + | |
| 2629 | + | |
| 2630 | + | |
| 2631 | + | |
| 2632 | + | |
| 2633 | + | |
| 2634 | + | |
| 2635 | + | |
2584 | 2636 | | |
2585 | 2637 | | |
2586 | 2638 | | |
| |||
2708 | 2760 | | |
2709 | 2761 | | |
2710 | 2762 | | |
| 2763 | + | |
| 2764 | + | |
| 2765 | + | |
| 2766 | + | |
| 2767 | + | |
| 2768 | + | |
| 2769 | + | |
| 2770 | + | |
| 2771 | + | |
| 2772 | + | |
| 2773 | + | |
| 2774 | + | |
2711 | 2775 | | |
2712 | 2776 | | |
2713 | 2777 | | |
| |||
2847 | 2911 | | |
2848 | 2912 | | |
2849 | 2913 | | |
| 2914 | + | |
| 2915 | + | |
| 2916 | + | |
| 2917 | + | |
| 2918 | + | |
| 2919 | + | |
| 2920 | + | |
| 2921 | + | |
| 2922 | + | |
| 2923 | + | |
| 2924 | + | |
| 2925 | + | |
2850 | 2926 | | |
2851 | 2927 | | |
2852 | 2928 | | |
| |||
2987 | 3063 | | |
2988 | 3064 | | |
2989 | 3065 | | |
| 3066 | + | |
| 3067 | + | |
| 3068 | + | |
| 3069 | + | |
| 3070 | + | |
| 3071 | + | |
| 3072 | + | |
| 3073 | + | |
| 3074 | + | |
| 3075 | + | |
| 3076 | + | |
| 3077 | + | |
2990 | 3078 | | |
2991 | 3079 | | |
2992 | 3080 | | |
| |||
0 commit comments