可以利用 useReducer
每次调用 updateReducer
方法,来达到强制组件重新渲染的目的。
import { useReducer } from 'react';
const updateReducer = (num: number): number => (num + 1) % 1_000_000;
export default function useUpdate(): () => void {
const [, update] = useReducer(updateReducer, 0);
return update;
}