Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何使用新道具更新模态组件_Javascript_Reactjs - Fatal编程技术网

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
  })
}