Skip to content

Instantly share code, notes, and snippets.

@pshaddel
Created April 13, 2024 20:38
Show Gist options
  • Save pshaddel/3f7926d7f16ba7f83c83925c3e3af08b to your computer and use it in GitHub Desktop.
Save pshaddel/3f7926d7f16ba7f83c83925c3e3af08b to your computer and use it in GitHub Desktop.
mountStateImpl
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 = 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>> = {
pending: null,
lanes: NoLanes,
dispatch: null,
lastRenderedReducer: basicStateReducer,
lastRenderedState: (initialState: any),
};
hook.queue = queue;
return hook;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment