Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DevTools: Add support for useOptimistic Hook #27982

Merged
merged 4 commits into from
Feb 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions packages/react-debug-tools/src/ReactDebugHooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,11 @@ function getPrimitiveStackCache(): Map<string, Array<any>> {
// This type check is for Flow only.
Dispatcher.useMemoCache(0);
}

if (typeof Dispatcher.useOptimistic === 'function') {
// This type check is for Flow only.
Dispatcher.useOptimistic(null, (s: mixed, a: mixed) => s);
}
} finally {
readHookLog = hookLog;
hookLog = [];
Expand Down Expand Up @@ -348,6 +353,25 @@ function useMemoCache(size: number): Array<any> {
return data;
}

function useOptimistic<S, A>(
passthrough: S,
reducer: ?(S, A) => S,
): [S, (A) => void] {
const hook = nextHook();
let state;
if (hook !== null) {
state = hook.memoizedState;
} else {
state = passthrough;
}
hookLog.push({
primitive: 'Optimistic',
stackError: new Error(),
value: state,
});
return [state, (action: A) => {}];
}

const Dispatcher: DispatcherType = {
use,
readContext,
Expand All @@ -361,6 +385,7 @@ const Dispatcher: DispatcherType = {
useInsertionEffect,
useMemo,
useMemoCache,
useOptimistic,
useReducer,
useRef,
useState,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1106,4 +1106,42 @@ describe('ReactHooksInspectionIntegration', () => {
},
]);
});

// @gate enableAsyncActions
it('should support useOptimistic hook', () => {
const useOptimistic = React.useOptimistic;
function Foo() {
const [value] = useOptimistic('abc', currentState => currentState);
React.useMemo(() => 'memo', []);
React.useMemo(() => 'not used', []);
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: 'Optimistic',
value: 'abc',
subHooks: [],
},
{
id: 1,
isStateEditable: false,
name: 'Memo',
value: 'memo',
subHooks: [],
},
{
id: 2,
isStateEditable: false,
name: 'Memo',
value: 'not used',
subHooks: [],
},
]);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
useContext,
useDebugValue,
useEffect,
useOptimistic,
useState,
} from 'react';

Expand Down Expand Up @@ -73,6 +74,7 @@ function FunctionWithHooks(props: any, ref: React$Ref<any>) {
const [count, updateCount] = useState(0);
// eslint-disable-next-line no-unused-vars
const contextValueA = useContext(ContextA);
useOptimistic<number, mixed>(1);

// eslint-disable-next-line no-unused-vars
const [_, __] = useState(object);
Expand Down