-
Notifications
You must be signed in to change notification settings - Fork 47.4k
/
Copy pathuseSyncExternalStoreShared-test.js
1079 lines (1055 loc) · 32.3 KB
/
useSyncExternalStoreShared-test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @emails react-core
*/
'use strict';
let useSyncExternalStore;
let useSyncExternalStoreWithSelector;
let React;
let ReactDOM;
let ReactDOMClient;
let Scheduler;
let act;
let useState;
let useEffect;
let useLayoutEffect;
let assertLog;
let assertConsoleErrorDev;
// This tests shared behavior between the built-in and shim implementations of
// of useSyncExternalStore.
describe('Shared useSyncExternalStore behavior (shim and built-in)', () => {
beforeEach(() => {
jest.resetModules();
if (gate(flags => flags.enableUseSyncExternalStoreShim)) {
// Test the shim against React 17.
jest.mock('react', () => {
return jest.requireActual(
__DEV__
? 'react-17/umd/react.development.js'
: 'react-17/umd/react.production.min.js',
);
});
jest.mock('react-dom', () =>
jest.requireActual(
__DEV__
? 'react-dom-17/umd/react-dom.development.js'
: 'react-dom-17/umd/react-dom.production.min.js',
),
);
jest.mock('react-dom/client', () =>
jest.requireActual(
__DEV__
? 'react-dom-17/umd/react-dom.development.js'
: 'react-dom-17/umd/react-dom.production.min.js',
),
);
}
React = require('react');
ReactDOM = require('react-dom');
ReactDOMClient = require('react-dom/client');
Scheduler = require('scheduler');
useState = React.useState;
useEffect = React.useEffect;
useLayoutEffect = React.useLayoutEffect;
const InternalTestUtils = require('internal-test-utils');
assertLog = InternalTestUtils.assertLog;
assertConsoleErrorDev = InternalTestUtils.assertConsoleErrorDev;
const internalAct = require('internal-test-utils').act;
// The internal act implementation doesn't batch updates by default, since
// it's mostly used to test concurrent mode. But since these tests run
// in both concurrent and legacy mode, I'm adding batching here.
act = cb => internalAct(() => ReactDOM.unstable_batchedUpdates(cb));
if (gate(flags => flags.source)) {
// The `shim/with-selector` module composes the main
// `use-sync-external-store` entrypoint. In the compiled artifacts, this
// is resolved to the `shim` implementation by our build config, but when
// running the tests against the source files, we need to tell Jest how to
// resolve it. Because this is a source module, this mock has no affect on
// the build tests.
jest.mock('use-sync-external-store/src/useSyncExternalStore', () =>
jest.requireActual('use-sync-external-store/shim'),
);
}
useSyncExternalStore =
require('use-sync-external-store/shim').useSyncExternalStore;
useSyncExternalStoreWithSelector =
require('use-sync-external-store/shim/with-selector').useSyncExternalStoreWithSelector;
});
function Text({text}) {
Scheduler.log(text);
return text;
}
function createRoot(container) {
// This wrapper function exists so we can test both legacy roots and
// concurrent roots.
if (gate(flags => !flags.enableUseSyncExternalStoreShim)) {
// The native implementation only exists in 18+, so we test using
// concurrent mode. To test the legacy root behavior in the native
// implementation (which is supported in the sense that it needs to have
// the correct behavior, despite the fact that the legacy root API
// triggers a warning in 18), write a test that uses
// createLegacyRoot directly.
return ReactDOMClient.createRoot(container);
} else {
// This ReactDOM.render is from the React 17 npm module.
ReactDOM.render(null, container);
return {
render(children) {
ReactDOM.render(children, container);
},
};
}
}
function createExternalStore(initialState) {
const listeners = new Set();
let currentState = initialState;
return {
set(text) {
currentState = text;
ReactDOM.unstable_batchedUpdates(() => {
listeners.forEach(listener => listener());
});
},
subscribe(listener) {
listeners.add(listener);
return () => listeners.delete(listener);
},
getState() {
return currentState;
},
getSubscriberCount() {
return listeners.size;
},
};
}
it('basic usage', async () => {
const store = createExternalStore('Initial');
function App() {
const text = useSyncExternalStore(store.subscribe, store.getState);
return React.createElement(Text, {
text: text,
});
}
const container = document.createElement('div');
const root = createRoot(container);
await act(() => root.render(React.createElement(App, null)));
assertLog(['Initial']);
expect(container.textContent).toEqual('Initial');
await act(() => {
store.set('Updated');
});
assertLog(['Updated']);
expect(container.textContent).toEqual('Updated');
});
it('skips re-rendering if nothing changes', async () => {
const store = createExternalStore('Initial');
function App() {
const text = useSyncExternalStore(store.subscribe, store.getState);
return React.createElement(Text, {
text: text,
});
}
const container = document.createElement('div');
const root = createRoot(container);
await act(() => root.render(React.createElement(App, null)));
assertLog(['Initial']);
expect(container.textContent).toEqual('Initial');
// Update to the same value
await act(() => {
store.set('Initial');
});
// Should not re-render
assertLog([]);
expect(container.textContent).toEqual('Initial');
});
it('switch to a different store', async () => {
const storeA = createExternalStore(0);
const storeB = createExternalStore(0);
let setStore;
function App() {
const [store, _setStore] = useState(storeA);
setStore = _setStore;
const value = useSyncExternalStore(store.subscribe, store.getState);
return React.createElement(Text, {
text: value,
});
}
const container = document.createElement('div');
const root = createRoot(container);
await act(() => root.render(React.createElement(App, null)));
assertLog([0]);
expect(container.textContent).toEqual('0');
await act(() => {
storeA.set(1);
});
assertLog([1]);
expect(container.textContent).toEqual('1');
// Switch stores and update in the same batch
await act(() => {
ReactDOM.flushSync(() => {
// This update will be disregarded
storeA.set(2);
setStore(storeB);
});
});
// Now reading from B instead of A
assertLog([0]);
expect(container.textContent).toEqual('0');
// Update A
await act(() => {
storeA.set(3);
});
// Nothing happened, because we're no longer subscribed to A
assertLog([]);
expect(container.textContent).toEqual('0');
// Update B
await act(() => {
storeB.set(1);
});
assertLog([1]);
expect(container.textContent).toEqual('1');
});
it('selecting a specific value inside getSnapshot', async () => {
const store = createExternalStore({
a: 0,
b: 0,
});
function A() {
const a = useSyncExternalStore(store.subscribe, () => store.getState().a);
return React.createElement(Text, {
text: 'A' + a,
});
}
function B() {
const b = useSyncExternalStore(store.subscribe, () => store.getState().b);
return React.createElement(Text, {
text: 'B' + b,
});
}
function App() {
return React.createElement(
React.Fragment,
null,
React.createElement(A, null),
React.createElement(B, null),
);
}
const container = document.createElement('div');
const root = createRoot(container);
await act(() => root.render(React.createElement(App, null)));
assertLog(['A0', 'B0']);
expect(container.textContent).toEqual('A0B0');
// Update b but not a
await act(() => {
store.set({
a: 0,
b: 1,
});
});
// Only b re-renders
assertLog(['B1']);
expect(container.textContent).toEqual('A0B1');
// Update a but not b
await act(() => {
store.set({
a: 1,
b: 1,
});
});
// Only a re-renders
assertLog(['A1']);
expect(container.textContent).toEqual('A1B1');
});
// In React 18, you can't observe in between a sync render and its
// passive effects, so this is only relevant to legacy roots
// @gate enableUseSyncExternalStoreShim
it(
"compares to current state before bailing out, even when there's a " +
'mutation in between the sync and passive effects',
async () => {
const store = createExternalStore(0);
function App() {
const value = useSyncExternalStore(store.subscribe, store.getState);
useEffect(() => {
Scheduler.log('Passive effect: ' + value);
}, [value]);
return React.createElement(Text, {
text: value,
});
}
const container = document.createElement('div');
const root = createRoot(container);
await act(() => root.render(React.createElement(App, null)));
assertLog([0, 'Passive effect: 0']);
// Schedule an update. We'll intentionally not use `act` so that we can
// insert a mutation before React subscribes to the store in a
// passive effect.
store.set(1);
assertLog([
1,
// Passive effect hasn't fired yet
]);
expect(container.textContent).toEqual('1');
// Flip the store state back to the previous value.
store.set(0);
assertLog([
'Passive effect: 1',
// Re-render. If the current state were tracked by updating a ref in a
// passive effect, then this would break because the previous render's
// passive effect hasn't fired yet, so we'd incorrectly think that
// the state hasn't changed.
0,
]);
// Should flip back to 0
expect(container.textContent).toEqual('0');
},
);
it('mutating the store in between render and commit when getSnapshot has changed', async () => {
const store = createExternalStore({
a: 1,
b: 1,
});
const getSnapshotA = () => store.getState().a;
const getSnapshotB = () => store.getState().b;
function Child1({step}) {
const value = useSyncExternalStore(store.subscribe, store.getState);
useLayoutEffect(() => {
if (step === 1) {
// Update B in a layout effect. This happens in the same commit
// that changed the getSnapshot in Child2. Child2's effects haven't
// fired yet, so it doesn't have access to the latest getSnapshot. So
// it can't use the getSnapshot to bail out.
Scheduler.log('Update B in commit phase');
store.set({
a: value.a,
b: 2,
});
}
}, [step]);
return null;
}
function Child2({step}) {
const label = step === 0 ? 'A' : 'B';
const getSnapshot = step === 0 ? getSnapshotA : getSnapshotB;
const value = useSyncExternalStore(store.subscribe, getSnapshot);
return React.createElement(Text, {
text: label + value,
});
}
let setStep;
function App() {
const [step, _setStep] = useState(0);
setStep = _setStep;
return React.createElement(
React.Fragment,
null,
React.createElement(Child1, {
step: step,
}),
React.createElement(Child2, {
step: step,
}),
);
}
const container = document.createElement('div');
const root = createRoot(container);
await act(() => root.render(React.createElement(App, null)));
assertLog(['A1']);
expect(container.textContent).toEqual('A1');
await act(() => {
// Change getSnapshot and update the store in the same batch
setStep(1);
});
assertLog([
'B1',
'Update B in commit phase',
// If Child2 had used the old getSnapshot to bail out, then it would have
// incorrectly bailed out here instead of re-rendering.
'B2',
]);
expect(container.textContent).toEqual('B2');
});
it('mutating the store in between render and commit when getSnapshot has _not_ changed', async () => {
// Same as previous test, but `getSnapshot` does not change
const store = createExternalStore({
a: 1,
b: 1,
});
const getSnapshotA = () => store.getState().a;
function Child1({step}) {
const value = useSyncExternalStore(store.subscribe, store.getState);
useLayoutEffect(() => {
if (step === 1) {
// Update B in a layout effect. This happens in the same commit
// that changed the getSnapshot in Child2. Child2's effects haven't
// fired yet, so it doesn't have access to the latest getSnapshot. So
// it can't use the getSnapshot to bail out.
Scheduler.log('Update B in commit phase');
store.set({
a: value.a,
b: 2,
});
}
}, [step]);
return null;
}
function Child2({step}) {
const value = useSyncExternalStore(store.subscribe, getSnapshotA);
return React.createElement(Text, {
text: 'A' + value,
});
}
let setStep;
function App() {
const [step, _setStep] = useState(0);
setStep = _setStep;
return React.createElement(
React.Fragment,
null,
React.createElement(Child1, {
step: step,
}),
React.createElement(Child2, {
step: step,
}),
);
}
const container = document.createElement('div');
const root = createRoot(container);
await act(() => root.render(React.createElement(App, null)));
assertLog(['A1']);
expect(container.textContent).toEqual('A1');
// This will cause a layout effect, and in the layout effect we'll update
// the store
await act(() => {
setStep(1);
});
assertLog([
'A1',
// This updates B, but since Child2 doesn't subscribe to B, it doesn't
// need to re-render.
'Update B in commit phase',
// No re-render
]);
expect(container.textContent).toEqual('A1');
});
it("does not bail out if the previous update hasn't finished yet", async () => {
const store = createExternalStore(0);
function Child1() {
const value = useSyncExternalStore(store.subscribe, store.getState);
useLayoutEffect(() => {
if (value === 1) {
Scheduler.log('Reset back to 0');
store.set(0);
}
}, [value]);
return React.createElement(Text, {
text: value,
});
}
function Child2() {
const value = useSyncExternalStore(store.subscribe, store.getState);
return React.createElement(Text, {
text: value,
});
}
const container = document.createElement('div');
const root = createRoot(container);
await act(() =>
root.render(
React.createElement(
React.Fragment,
null,
React.createElement(Child1, null),
React.createElement(Child2, null),
),
),
);
assertLog([0, 0]);
expect(container.textContent).toEqual('00');
await act(() => {
store.set(1);
});
assertLog([1, 1, 'Reset back to 0', 0, 0]);
expect(container.textContent).toEqual('00');
});
it('uses the latest getSnapshot, even if it changed in the same batch as a store update', async () => {
const store = createExternalStore({
a: 0,
b: 0,
});
const getSnapshotA = () => store.getState().a;
const getSnapshotB = () => store.getState().b;
let setGetSnapshot;
function App() {
const [getSnapshot, _setGetSnapshot] = useState(() => getSnapshotA);
setGetSnapshot = _setGetSnapshot;
const text = useSyncExternalStore(store.subscribe, getSnapshot);
return React.createElement(Text, {
text: text,
});
}
const container = document.createElement('div');
const root = createRoot(container);
await act(() => root.render(React.createElement(App, null)));
assertLog([0]);
// Update the store and getSnapshot at the same time
await act(() => {
ReactDOM.flushSync(() => {
setGetSnapshot(() => getSnapshotB);
store.set({
a: 1,
b: 2,
});
});
});
// It should read from B instead of A
assertLog([2]);
expect(container.textContent).toEqual('2');
});
it('handles errors thrown by getSnapshot', async () => {
class ErrorBoundary extends React.Component {
state = {
error: null,
};
static getDerivedStateFromError(error) {
return {
error,
};
}
render() {
if (this.state.error) {
return React.createElement(Text, {
text: this.state.error.message,
});
}
return this.props.children;
}
}
const store = createExternalStore({
value: 0,
throwInGetSnapshot: false,
throwInIsEqual: false,
});
function App() {
const {value} = useSyncExternalStore(store.subscribe, () => {
const state = store.getState();
if (state.throwInGetSnapshot) {
throw new Error('Error in getSnapshot');
}
return state;
});
return React.createElement(Text, {
text: value,
});
}
const errorBoundary = React.createRef(null);
const container = document.createElement('div');
const root = createRoot(container);
await act(() =>
root.render(
React.createElement(
ErrorBoundary,
{
ref: errorBoundary,
},
React.createElement(App, null),
),
),
);
assertLog([0]);
expect(container.textContent).toEqual('0');
// Update that throws in a getSnapshot. We can catch it with an error boundary.
if (__DEV__ && gate(flags => flags.enableUseSyncExternalStoreShim)) {
// In 17, the error is re-thrown in DEV.
await expect(async () => {
await act(() => {
store.set({
value: 1,
throwInGetSnapshot: true,
throwInIsEqual: false,
});
});
}).rejects.toThrow('Error in getSnapshot');
} else {
await act(() => {
store.set({
value: 1,
throwInGetSnapshot: true,
throwInIsEqual: false,
});
});
}
assertLog(
gate(flags => flags.enableUseSyncExternalStoreShim)
? ['Error in getSnapshot']
: [
'Error in getSnapshot',
// In a concurrent root, React renders a second time to attempt to
// recover from the error.
'Error in getSnapshot',
],
);
expect(container.textContent).toEqual('Error in getSnapshot');
});
it('Infinite loop if getSnapshot keeps returning new reference', async () => {
const store = createExternalStore({});
function App() {
const text = useSyncExternalStore(store.subscribe, () => ({}));
return React.createElement(Text, {
text: JSON.stringify(text),
});
}
const container = document.createElement('div');
const root = createRoot(container);
await expect(async () => {
await act(() => {
ReactDOM.flushSync(async () =>
root.render(React.createElement(App, null)),
);
});
}).rejects.toThrow(
'Maximum update depth exceeded. This can happen when a component repeatedly ' +
'calls setState inside componentWillUpdate or componentDidUpdate. React limits ' +
'the number of nested updates to prevent infinite loops.',
);
assertConsoleErrorDev(
gate(flags => flags.enableUseSyncExternalStoreShim)
? [
[
'The result of getSnapshot should be cached to avoid an infinite loop',
{withoutStack: true},
],
'Error: Maximum update depth exceeded',
'The above error occurred i',
]
: [
'The result of getSnapshot should be cached to avoid an infinite loop',
],
);
});
it('getSnapshot can return NaN without infinite loop warning', async () => {
const store = createExternalStore('not a number');
function App() {
const value = useSyncExternalStore(store.subscribe, () =>
parseInt(store.getState(), 10),
);
return React.createElement(Text, {
text: value,
});
}
const container = document.createElement('div');
const root = createRoot(container);
// Initial render that reads a snapshot of NaN. This is OK because we use
// Object.is algorithm to compare values.
await act(() => root.render(React.createElement(App, null)));
expect(container.textContent).toEqual('NaN');
assertLog([NaN]);
// Update to real number
await act(() => store.set(123));
expect(container.textContent).toEqual('123');
assertLog([123]);
// Update back to NaN
await act(() => store.set('not a number'));
expect(container.textContent).toEqual('NaN');
assertLog([NaN]);
});
describe('extra features implemented in user-space', () => {
it('memoized selectors are only called once per update', async () => {
const store = createExternalStore({
a: 0,
b: 0,
});
function selector(state) {
Scheduler.log('Selector');
return state.a;
}
function App() {
Scheduler.log('App');
const a = useSyncExternalStoreWithSelector(
store.subscribe,
store.getState,
null,
selector,
);
return React.createElement(Text, {
text: 'A' + a,
});
}
const container = document.createElement('div');
const root = createRoot(container);
await act(() => root.render(React.createElement(App, null)));
assertLog(['App', 'Selector', 'A0']);
expect(container.textContent).toEqual('A0');
// Update the store
await act(() => {
store.set({
a: 1,
b: 0,
});
});
assertLog([
// The selector runs before React starts rendering
'Selector',
'App',
// And because the selector didn't change during render, we can reuse
// the previous result without running the selector again
'A1',
]);
expect(container.textContent).toEqual('A1');
});
it('Using isEqual to bailout', async () => {
const store = createExternalStore({
a: 0,
b: 0,
});
function A() {
const {a} = useSyncExternalStoreWithSelector(
store.subscribe,
store.getState,
null,
state => ({
a: state.a,
}),
(state1, state2) => state1.a === state2.a,
);
return React.createElement(Text, {
text: 'A' + a,
});
}
function B() {
const {b} = useSyncExternalStoreWithSelector(
store.subscribe,
store.getState,
null,
state => {
return {
b: state.b,
};
},
(state1, state2) => state1.b === state2.b,
);
return React.createElement(Text, {
text: 'B' + b,
});
}
function App() {
return React.createElement(
React.Fragment,
null,
React.createElement(A, null),
React.createElement(B, null),
);
}
const container = document.createElement('div');
const root = createRoot(container);
await act(() => root.render(React.createElement(App, null)));
assertLog(['A0', 'B0']);
expect(container.textContent).toEqual('A0B0');
// Update b but not a
await act(() => {
store.set({
a: 0,
b: 1,
});
});
// Only b re-renders
assertLog(['B1']);
expect(container.textContent).toEqual('A0B1');
// Update a but not b
await act(() => {
store.set({
a: 1,
b: 1,
});
});
// Only a re-renders
assertLog(['A1']);
expect(container.textContent).toEqual('A1B1');
});
it('basic server hydration', async () => {
const store = createExternalStore('client');
const ref = React.createRef();
function App() {
const text = useSyncExternalStore(
store.subscribe,
store.getState,
() => 'server',
);
useEffect(() => {
Scheduler.log('Passive effect: ' + text);
}, [text]);
return React.createElement(
'div',
{
ref: ref,
},
React.createElement(Text, {
text: text,
}),
);
}
const container = document.createElement('div');
container.innerHTML = '<div>server</div>';
const serverRenderedDiv = container.getElementsByTagName('div')[0];
if (gate(flags => !flags.enableUseSyncExternalStoreShim)) {
await act(() => {
ReactDOMClient.hydrateRoot(container, React.createElement(App, null));
});
assertLog([
// First it hydrates the server rendered HTML
'server',
'Passive effect: server',
// Then in a second paint, it re-renders with the client state
'client',
'Passive effect: client',
]);
} else {
// In the userspace shim, there's no mechanism to detect whether we're
// currently hydrating, so `getServerSnapshot` is not called on the
// client. To avoid this server mismatch warning, user must account for
// this themselves and return the correct value inside `getSnapshot`.
await act(() => {
ReactDOM.hydrate(React.createElement(App, null), container);
});
assertConsoleErrorDev(['Text content did not match']);
assertLog(['client', 'Passive effect: client']);
}
expect(container.textContent).toEqual('client');
expect(ref.current).toEqual(serverRenderedDiv);
});
});
it('regression test for #23150', async () => {
const store = createExternalStore('Initial');
function App() {
const text = useSyncExternalStore(store.subscribe, store.getState);
const [derivedText, setDerivedText] = useState(text);
useEffect(() => {}, []);
if (derivedText !== text.toUpperCase()) {
setDerivedText(text.toUpperCase());
}
return React.createElement(Text, {
text: derivedText,
});
}
const container = document.createElement('div');
const root = createRoot(container);
await act(() => root.render(React.createElement(App, null)));
assertLog(['INITIAL']);
expect(container.textContent).toEqual('INITIAL');
await act(() => {
store.set('Updated');
});
assertLog(['UPDATED']);
expect(container.textContent).toEqual('UPDATED');
});
it('compares selection to rendered selection even if selector changes', async () => {
const store = createExternalStore({
items: ['A', 'B'],
});
const shallowEqualArray = (a, b) => {
if (a.length !== b.length) {
return false;
}
for (let i = 0; i < a.length; i++) {
if (a[i] !== b[i]) {
return false;
}
}
return true;
};
const List = React.memo(({items}) => {
return React.createElement(
'ul',
null,
items.map(text =>
React.createElement(
'li',
{
key: text,
},
React.createElement(Text, {
key: text,
text: text,
}),
),
),
);
});
function App({step}) {
const inlineSelector = state => {
Scheduler.log('Inline selector');
return [...state.items, 'C'];
};
const items = useSyncExternalStoreWithSelector(
store.subscribe,
store.getState,
null,
inlineSelector,
shallowEqualArray,
);
return React.createElement(
React.Fragment,
null,
React.createElement(List, {
items: items,
}),
React.createElement(Text, {
text: 'Sibling: ' + step,
}),
);
}
const container = document.createElement('div');
const root = createRoot(container);
await act(() => {
root.render(
React.createElement(App, {
step: 0,
}),
);
});
assertLog(['Inline selector', 'A', 'B', 'C', 'Sibling: 0']);
await act(() => {
root.render(
React.createElement(App, {
step: 1,
}),
);
});
assertLog([
// We had to call the selector again because it's not memoized
'Inline selector',
// But because the result was the same (according to isEqual) we can
// bail out of rendering the memoized list. These are skipped:
// 'A',
// 'B',
// 'C',
'Sibling: 1',
]);
});
describe('selector and isEqual error handling in extra', () => {
let ErrorBoundary;
beforeEach(() => {
ErrorBoundary = class extends React.Component {
state = {
error: null,
};
static getDerivedStateFromError(error) {
return {
error,
};
}
render() {
if (this.state.error) {
return React.createElement(Text, {
text: this.state.error.message,
});
}
return this.props.children;
}
};
});
it('selector can throw on update', async () => {
const store = createExternalStore({
a: 'a',
});
const selector = state => {
if (typeof state.a !== 'string') {
throw new TypeError('Malformed state');
}
return state.a.toUpperCase();
};
function App() {
const a = useSyncExternalStoreWithSelector(
store.subscribe,
store.getState,
null,
selector,
);
return React.createElement(Text, {
text: a,
});
}