You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
functionmountMemo<T>(nextCreate: () =>T,deps: Array<mixed>|void|null): T{consthook=mountWorkInProgressHook();constnextDeps=deps===undefined ? null : deps;if(shouldDoubleInvokeUserFnsInHooksDEV){nextCreate();}constnextValue=nextCreate();hook.memoizedState=[nextValue,nextDeps];returnnextValue;}functionupdateMemo<T>(nextCreate: () =>T,deps: Array<mixed>|void|null): T{consthook=updateWorkInProgressHook();constnextDeps=deps===undefined ? null : deps;constprevState=hook.memoizedState;// Assume these are defined. If they're not, areHookInputsEqual will warn.if(nextDeps!==null){constprevDeps: Array<mixed> | null = prevState[1];
if (areHookInputsEqual(nextDeps, prevDeps)) {returnprevState[0];}}if(shouldDoubleInvokeUserFnsInHooksDEV){nextCreate();}constnextValue=nextCreate();hook.memoizedState=[nextValue,nextDeps];returnnextValue;}
areHookInputsEqual
importisfrom'shared/objectIs';functionareHookInputsEqual(nextDeps: Array<mixed>,prevDeps: Array<mixed>|null): boolean{if(__DEV__){if(ignorePreviousDependencies){// Only true when this component is being hot reloaded.returnfalse;}}if(prevDeps===null){if(__DEV__){console.error('%s received a final argument during this render, but not during '+'the previous render. Even though the final argument is optional, '+'its type cannot change between renders.',currentHookNameInDev,);}returnfalse;}if(__DEV__){// Don't bother comparing lengths in prod because these arrays should be// passed inline.if(nextDeps.length!==prevDeps.length){console.error('The final argument passed to %s changed size between renders. The '+'order and size of this array must remain constant.\n\n'+'Previous: %s\n'+'Incoming: %s',currentHookNameInDev,`[${prevDeps.join(', ')}]`,`[${nextDeps.join(', ')}]`,);}}// $FlowFixMe[incompatible-use] found when upgrading Flowfor(leti=0;i<prevDeps.length&&i<nextDeps.length;i++){// $FlowFixMe[incompatible-use] found when upgrading Flowif(is(nextDeps[i],prevDeps[i])){continue;}returnfalse;}returntrue;}
is
/** * inlined Object.is polyfill to avoid requiring consumers ship their own * https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/is */functionis(x: any,y: any){return((x===y&&(x!==0||1/x===1/y))||(x!==x&&y!==y)// eslint-disable-line no-self-compare);}constobjectIs: (x: any,y: any)=>boolean=// $FlowFixMe[method-unbinding]typeofObject.is==='function' ? Object.is : is;exportdefaultobjectIs;
The text was updated successfully, but these errors were encountered:
React18 源码解析之 useCallback 和 useMemo
useCallback
useMemo
areHookInputsEqual
is
The text was updated successfully, but these errors were encountered: