shfshanyue / Daily-Question

互联网大厂内推及大厂面经整理,并且每天一道面试题推送。每天五分钟,半年大厂中
https://q.shanyue.tech
4.95k stars 510 forks source link

【Q600】在 React hooks 中如何模拟 forceUpdate #616

Open shfshanyue opened 3 years ago

shfshanyue commented 3 years ago
  const [ignored, forceUpdate] = useReducer(x => x + 1, 0);

  function handleClick() {
    forceUpdate();
  }
wongchisum commented 1 year ago
import {useState,useCallback} from 'react';

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

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