react17和react18中useReducer为什么会表现不一致?

export default function App() {
  const [count,dispatch] = useReducer((prevState)=>{
    console.log('reducer')
    return prevState+1
  },12)
  return (
    <div className="App">
    <h1 onClick={()=>{
      console.log('dispatch-start')
      dispatch()
      console.log('dispatch-end')
    }}>Hello CodeSandbox{count}</h1>
      <h2>Start editing to see some magic happen!</h2>
    </div>
  );
}
  • 在react17中点击h1第一次会打印
    dispatch-start
    reducer
    dispatch-end
    reducer
  • 如果再次点击h1会打印
    dispatch-start
    dispatch-end
    reducer
  • 如果在react18中每次打印出的都是
    dispatch-start
    dispatch-end
    reducer

这是什么原因呢?

阅读 1.5k
1 个回答
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题