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

Ensure useState and useReducer initializer functions are double invoked in StrictMode #28248

Merged
merged 5 commits into from
Feb 6, 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
14 changes: 13 additions & 1 deletion packages/react-reconciler/src/ReactFiberHooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -1158,6 +1158,11 @@ function mountReducer<S, I, A>(
let initialState;
if (init !== undefined) {
initialState = init(initialArg);
if (shouldDoubleInvokeUserFnsInHooksDEV) {
setIsStrictModeForDevtools(true);
init(initialArg);
setIsStrictModeForDevtools(false);
}
} else {
initialState = ((initialArg: any): S);
}
Expand Down Expand Up @@ -1749,8 +1754,15 @@ function forceStoreRerender(fiber: Fiber) {
function mountStateImpl<S>(initialState: (() => S) | S): Hook {
const hook = mountWorkInProgressHook();
if (typeof initialState === 'function') {
const initialStateInitializer = initialState;
// $FlowFixMe[incompatible-use]: Flow doesn't like mixed types
initialState = initialState();
initialState = initialStateInitializer();
if (shouldDoubleInvokeUserFnsInHooksDEV) {
setIsStrictModeForDevtools(true);
// $FlowFixMe[incompatible-use]: Flow doesn't like mixed types
initialStateInitializer();
setIsStrictModeForDevtools(false);
}
}
hook.memoizedState = hook.baseState = initialState;
const queue: UpdateQueue<S, BasicStateAction<S>> = {
Expand Down
40 changes: 40 additions & 0 deletions packages/react/src/__tests__/ReactStrictMode-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,46 @@ describe('ReactStrictMode', () => {
expect(instance.state.count).toBe(2);
});

// @gate debugRenderPhaseSideEffectsForStrictMode
it('double invokes useState and useReducer initializers functions', async () => {
const log = [];

function App() {
React.useState(() => {
log.push('Compute initial state count: 1');
return 1;
});
React.useReducer(
s => s,
2,
s => {
log.push('Compute initial reducer count: 2');
return s;
},
);

return 3;
}

const container = document.createElement('div');
const root = ReactDOMClient.createRoot(container);
await act(() => {
root.render(
<React.StrictMode>
<App />
</React.StrictMode>,
);
});
expect(container.textContent).toBe('3');

expect(log).toEqual([
'Compute initial state count: 1',
'Compute initial state count: 1',
'Compute initial reducer count: 2',
'Compute initial reducer count: 2',
]);
});

it('should invoke only precommit lifecycle methods twice in DEV legacy roots', async () => {
const {StrictMode} = React;

Expand Down