forceUpdate setState有什么区别

请问forceUpdate setState有什么区别呢?
什么时候用forceUpdate?
求解?

阅读 3.6k
2 个回答

有些时候你的变量并不在 state 上,比如 this.couter 等,当这些变量改变是 React 组件是并不会重新执行 render 的,因此需要组件强制调用 render 方法。

forceUpdate会跳过shouldComponentUpdate步骤直接触发组件的render方法
官网建议应该尽量避免使用forceUpdate

Normally you should try to avoid all uses of forceUpdate() and only read from this.props and this.state in render().
https://reactjs.org/docs/reac...
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题