هناك نوعان من الظروف: التحميل والتحديث، لذا فإن useMemo له تطبيقان: mountMemo و updateMemo.
function mountMemo( nextCreate: () => T, deps: Array | void | null, ): T { const hook = mountWorkInProgressHook(); const nextDeps = deps === undefined ? null : deps; const nextValue = nextCreate(); if (shouldDoubleInvokeUserFnsInHooksDEV) { setIsStrictModeForDevtools(true); nextCreate(); setIsStrictModeForDevtools(false); } hook.memoizedState = [nextValue, nextDeps]; return nextValue; }
توضيح:
في مرحلة التحميل، تستدعي الدالة useMemo دالة رد الاتصال لحساب القيمة وإرجاعها. احفظ القيمة وتوجه إلى ملف Hook.memoizedState.
استخدم mountWorkInProgressHook لإنشاء كائن ربط.
احفظ deps في nextDeps.
اتصل بـ nextCreate () للحصول على nextValue. إذا كنت في بيئة تطوير، اتصل مرتين.
احفظ القيمة التالية وnextDeps في الخطاف.memoizedStateوإرجاع القيمة التالية.
function updateMemo( nextCreate: () => T, deps: Array | void | null, ): T { const hook = updateWorkInProgressHook(); const nextDeps = deps === undefined ? null : deps; const prevState = hook.memoizedState; // Assume these are defined. If they're not, areHookInputsEqual will warn. if (nextDeps !== null) { const prevDeps: Array | null = prevState[1]; if (areHookInputsEqual(nextDeps, prevDeps)) { return prevState[0]; } } const nextValue = nextCreate(); if (shouldDoubleInvokeUserFnsInHooksDEV) { setIsStrictModeForDevtools(true); nextCreate(); setIsStrictModeForDevtools(false); } hook.memoizedState = [nextValue, nextDeps]; return nextValue; }
توضيح:
في مرحلة التحديث، ستحكم React على ما إذا كانت عمليات الحذف قد تغيرت أم لا، وإذا تغيرت، ستقوم React بتشغيل رد الاتصال للحصول على القيمة الجديدة والعودة. إذا لم يتم تغييرها، فسوف تُرجع React القيمة القديمة.
تنصل: جميع الموارد المقدمة هي جزئيًا من الإنترنت. إذا كان هناك أي انتهاك لحقوق الطبع والنشر الخاصة بك أو الحقوق والمصالح الأخرى، فيرجى توضيح الأسباب التفصيلية وتقديم دليل على حقوق الطبع والنشر أو الحقوق والمصالح ثم إرسالها إلى البريد الإلكتروني: [email protected]. سوف نتعامل مع الأمر لك في أقرب وقت ممكن.
Copyright© 2022 湘ICP备2022001581号-3