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

在 React hooks 中如何模拟 forceUpdate

Issue 欢迎在 Gtihub Issue 中回答此问题: Issue 616 (opens in a new tab)

Author 回答者: shfshanyue (opens in a new tab)

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

Author 回答者: wongchisum (opens in a new tab)

import {useState,useCallback} from 'react';

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

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