我正在关注React教程。在那里,讲师没有使用直接通过this.props.errors
返回道具还原的错误。
相反,他在componentWillRecieveProps中将props.errors设置为状态。
componentWillRecieveProps(nextProps) {
if (nextProps.errors) {
this.setState({ errors: nextProps.errors})
}
}
这样做有什么意义?为什么不直接使用this.props.errors?
答案 0 :(得分:1)
通常这是个坏主意,您可能希望直接使用props
。
您要实现的目标:
两者的关键是,对于任何数据,您都需要选择一个 拥有它作为真理来源的单个组件,避免 将其复制到其他组件中。
如果将道具复制到状态,则将值存储在两个不同的位置。
请阅读整个文档页面以查看解决方案
答案 1 :(得分:0)
是直接在儿童中使用道具还是将道具存储在状态中取决于您将如何使用道具