Javascript 如何使用新道具更新模态组件
我有一个模态组件,用于将食谱保存到列表中Javascript 如何使用新道具更新模态组件,javascript,reactjs,Javascript,Reactjs,我有一个模态组件,用于将食谱保存到列表中 modal = <Modal saveRecipe={this.saveRecipe} closeModal={this.toggleModal}/> 而不是此.state。但是表单字段将变得不可编辑。所以,我试图改变模态组件的状态。在我的代码中,组件将接收道具是否从未被激发?在这种情况下,组件将如何接收道具?如何使用新的道具更新模态组件?在您的情况下,您必须使用componentDidMount,因为当设置editItem状态时,您将卸
modal = <Modal saveRecipe={this.saveRecipe} closeModal={this.toggleModal}/>
而不是
此.state
。但是表单字段将变得不可编辑。所以,我试图改变模态组件的状态。在我的代码中,组件将接收道具
是否从未被激发?在这种情况下,组件将如何接收道具?如何使用新的道具更新模态组件?在您的情况下,您必须使用componentDidMount
,因为当设置editItem
状态时,您将卸载Model
组件并使用内容重新安装它。因此,在这种情况下,componentwillreceiveprops
将不会被激发,但componentDidMount
将被激发。因此,将您的组件willreceiveprops
逻辑移动到componentDidMount
类似的位置
componentDidMount(){
console.log(this.props) // Never fired..why??
let {dish, ingredients} = this.props.content
this.setState({
dish:dish, ingredients:ingredients
})
}
在您的情况下,您必须使用componentDidMount
,因为当设置editItem
状态时,您将卸载Model
组件,并使用content
prop重新安装它。因此,在这种情况下,componentwillreceiveprops
将不会被激发,但componentDidMount
将被激发。因此,将您的组件willreceiveprops
逻辑移动到componentDidMount
类似的位置
componentDidMount(){
console.log(this.props) // Never fired..why??
let {dish, ingredients} = this.props.content
this.setState({
dish:dish, ingredients:ingredients
})
}
let {dish, ingredients} = this.props
componentDidMount(){
console.log(this.props) // Never fired..why??
let {dish, ingredients} = this.props.content
this.setState({
dish:dish, ingredients:ingredients
})
}