Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/21.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
Reactjs 更换道具时组件未点火_Reactjs_React Redux - Fatal编程技术网

Reactjs 更换道具时组件未点火

Reactjs 更换道具时组件未点火,reactjs,react-redux,Reactjs,React Redux,我的react应用程序上有一个嵌套组件 class Home extends React.Component { constructor() { super(); this.state = {id : "1"}; } render() { return <ShowChart id={this.state.id}/>; } } class Home扩展了React.Component{ 构造函数(){ 超级(); this.

我的react应用程序上有一个嵌套组件

class Home extends React.Component {
  constructor() {
    super();
    this.state = {id : "1"};
  }
  render() {
    return <ShowChart id={this.state.id}/>;
  }
}
class Home扩展了React.Component{
构造函数(){
超级();
this.state={id:“1”};
}
render(){
返回;
}
}
我从家中呈现组件展示图,并将this.state.id作为道具传递。ShowChart是具有redux connect()的组件

类图表扩展了React.Component{
构造函数(){
超级();
this.state={color:“red”};
}
componentDidMount(){
//API_调用(this.props.id)
//此API调用使用props.id作为参数,redux存储提供API响应
}
render(){
返回({API_response});
}
}
函数MapStateTops(状态){
返回{
//
};
}
功能图DispatchToprops(调度){
返回{
//    
}
}
导出默认连接(MapStateTrops、mapDispatchToProps)(图表)
我希望ShowChart在主组件中的值“id”发生更改时激发其componentDidMount。但它的表现似乎并非如此。我尝试了getDerivedStateFromProps()、componentDidUpdate()方法,但没有成功。 欢迎提出任何解决方案或建议。

查看以下答案:

另外,为什么不在构造函数内部进行API调用呢?因为它们已经在那里可用。

请查看以下答案:

另外,为什么不在构造函数内部进行API调用呢?因为它们已经在那里可用。

当组件装载到dom时,ComponentDidMount()只会触发一次,ComponentDidUpdate在ComponentDidMount()之后调用,并且在状态/道具更改时可用于执行某些操作

componentDidUpdate(prevProps, prevState) {
  //api call
  //state update
}
我认为您要做的是在id更改时重新呈现ShowCart,如果可以的话

class Home extends React.Component {
  constructor() {
    super();
    this.state = {id : "1"};
  }
  render() {
    return <ShowChart id={this.state.id} key={this.state.id}/>;
  }
}
class Home扩展了React.Component{
构造函数(){
超级();
this.state={id:“1”};
}
render(){
返回;
}
}
向组件添加键将在id值更改时强制其重新渲染

ComponentDidMount()仅在将组件装入dom时触发一次,ComponentDidUpdate在ComponentDidMount()之后调用,在状态/道具更改时可用于执行某些操作

componentDidUpdate(prevProps, prevState) {
  //api call
  //state update
}
我认为您要做的是在id更改时重新呈现ShowCart,如果可以的话

class Home extends React.Component {
  constructor() {
    super();
    this.state = {id : "1"};
  }
  render() {
    return <ShowChart id={this.state.id} key={this.state.id}/>;
  }
}
class Home扩展了React.Component{
构造函数(){
超级();
this.state={id:“1”};
}
render(){
返回;
}
}

向组件添加键将在id值更改时强制其重新渲染显然,我没有正确使用生命周期方法

componentDidUpdate(prevProps) {
    if (this.props.id !== prevProps.id) {
       //API_Call(this.props.id)
    }
}

这就解决了问题。感谢大家。

显然,我没有正确使用生命周期方法

componentDidUpdate(prevProps) {
    if (this.props.id !== prevProps.id) {
       //API_Call(this.props.id)
    }
}

这就解决了问题。感谢大家。

componentDidMount仅在重新渲染后运行。组件仅通过状态更改而不是属性更改重新渲染。
如果要更改道具更改的状态,请使用
componentdiddupdate
getDerivedStateFromProps

componentDidMount
仅在重新渲染后运行。组件仅通过状态更改而不是属性更改重新渲染。
如果要更改道具更改的状态,请使用
componentdiddupdate
getDerivedStateFromProps

componentDidMount
在组件装载时调用一次且仅调用一次。对于更新,请使用
componentdiddupdate
componentDidMount
在组件装载时只调用一次。对于更新,请使用
componentdiddupdate