高级前端react【Q600】在 React hooks 中如何模拟 forceUpdate

在 React hooks 中如何模拟 forceUpdate

Issue 欢迎在 Gtihub Issue 中回答此问题: Issue 616

Author 回答者: shfshanyue

const [ignored, forceUpdate] = useReducer((x) => x + 1, 0);
 
function handleClick() {
  forceUpdate();
}

Author 回答者: wongchisum

import {useState,useCallback} from 'react';

const [state,setState] = useState({})

const handleForceUpdate = useCallback(() => {
    setState({})
},[])