mirror of
https://github.com/hanzoai/react.git
synced 2026-09-24 17:02:27 +00:00
Add useSyncExternalStore to react-debug-tools (#22240)
Adds support for useSyncExternalStore to react-debug-tools, which in turn adds support for React Devtools. Test plan: I added a test to ReactHooksInspectionIntegration, based on existing one for useMutableSource.
This commit is contained in:
+13
-1
@@ -269,7 +269,19 @@ function useSyncExternalStore<T>(
|
||||
subscribe: (() => void) => () => void,
|
||||
getSnapshot: () => T,
|
||||
): T {
|
||||
throw new Error('Not yet implemented');
|
||||
// useSyncExternalStore() composes multiple hooks internally.
|
||||
// Advance the current hook index the same number of times
|
||||
// so that subsequent hooks have the right memoized state.
|
||||
nextHook(); // SyncExternalStore
|
||||
nextHook(); // LayoutEffect
|
||||
nextHook(); // Effect
|
||||
const value = getSnapshot();
|
||||
hookLog.push({
|
||||
primitive: 'SyncExternalStore',
|
||||
stackError: new Error(),
|
||||
value,
|
||||
});
|
||||
return value;
|
||||
}
|
||||
|
||||
function useTransition(): [boolean, (() => void) => void] {
|
||||
|
||||
+33
@@ -878,4 +878,37 @@ describe('ReactHooksInspectionIntegration', () => {
|
||||
},
|
||||
]);
|
||||
});
|
||||
|
||||
// @gate experimental || www
|
||||
it('should support composite useSyncExternalStore hook', () => {
|
||||
const useSyncExternalStore = React.unstable_useSyncExternalStore;
|
||||
function Foo() {
|
||||
const value = useSyncExternalStore(
|
||||
() => () => {},
|
||||
() => 'snapshot',
|
||||
);
|
||||
React.useMemo(() => 'memo', []);
|
||||
return value;
|
||||
}
|
||||
|
||||
const renderer = ReactTestRenderer.create(<Foo />);
|
||||
const childFiber = renderer.root.findByType(Foo)._currentFiber();
|
||||
const tree = ReactDebugTools.inspectHooksOfFiber(childFiber);
|
||||
expect(tree).toEqual([
|
||||
{
|
||||
id: 0,
|
||||
isStateEditable: false,
|
||||
name: 'SyncExternalStore',
|
||||
value: 'snapshot',
|
||||
subHooks: [],
|
||||
},
|
||||
{
|
||||
id: 1,
|
||||
isStateEditable: false,
|
||||
name: 'Memo',
|
||||
value: 'memo',
|
||||
subHooks: [],
|
||||
},
|
||||
]);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user