/
dedaMazai
/
aboutMe
Обзор
Документация
Войти
/
dedaMazai
/
aboutMe
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
src/shared/lib/render/forceUpdate.tsx
37 строк
870 B
Andrey Chipizubov
fix
05 сен 2023, 09:22
05 сен 2023, 09:22
8214691
Код
Авторство
О чём код?
import { createContext, ReactNode, useContext, useMemo, useState } from 'react'; const ForceUpdateContext = createContext({ value: true, forceUpdate: () => {}, }); export const useForceUpdate = () => { const { forceUpdate } = useContext(ForceUpdateContext); return forceUpdate; }; export function ForceUpdateProvider({ children }: { children: ReactNode }) { const [value, setValue] = useState(true); const forceUpdate = () => { setValue((prev) => !prev); setTimeout(() => { setValue((prev) => !prev); }, 120); }; const valueContext = useMemo(() => { return { value, forceUpdate }; }, [value]); if (!value) { return null; } return ( <ForceUpdateContext.Provider value={valueContext}> {children} </ForceUpdateContext.Provider> ); }