強制的にレンダリングをさせるHooks
- 2021/04/28
- React
import { useState, useCallback } from 'react';
/** 強制的に再レンダリングさせる */
export const useForceUpdate = () => {
const [, forceUpdate] = useState<undefined | boolean>(undefined);
return useCallback(() => {
forceUpdate((s) => !s);
}, []);
};
使い方
const forceUpdate = useForceUpdate();
...
<p onClick={() => forceUpdate()}>強制的にアップデート</p>
- React will try to recreate this component tree from scratch using the error boundary you provided, ErrorBoundary.
- Next.jsでサイトマップを爆速で生成してくれるパッケージ