中等React
0 1

在React 的componentWillUpdate中是否可以直接修改 state的值?为什么?

【概念释义】

componentWillUpdate(已 UNSAFE_ 更名)在 render 前调用,此阶段不应 setState——会触发额外 render 循环或与即将进行的更新冲突。React 17+ 严格模式下不推荐依赖;应改用 componentDidUpdategetDerivedStateFromProps / Hooks useEffect 响应 props 变化。

【基础使用】

jsx
"qb-hl-comment">// 反例
UNSAFE_componentWillUpdate(nextProps) {
  if (nextProps.id !== this.

登录查看完整 1061

回答讨论

暂无讨论,登录后可抢先发言

本题库更多题目(50)