【发布时间】:2020-05-09 16:33:09
【问题描述】:
引发了 React.js 警告:
IndexPage:不建议将 props 直接分配给 state,因为对 props 的更新不会反映在 state 中。大多数情况下,直接使用 props 会更好。
我有以下代码:
class IndexPage extends React.Component<IProps, IState> {
constructor(props) {
super(props)
this.state = props
}
}
但是,当我将代码更改为:
class IndexPage extends React.Component<IProps, IState> {
constructor(props) {
super(props)
this.state = {...props}
}
}
警告消失了。
你能解释一下原因吗?
【问题讨论】:
-
这也解决了我的问题。
标签: reactjs warnings react-props react-component react-state