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 使用setState访问父组件中的数据集_Reactjs_React Redux - Fatal编程技术网

Reactjs 使用setState访问父组件中的数据集

Reactjs 使用setState访问父组件中的数据集,reactjs,react-redux,Reactjs,React Redux,我有一个父组件a,它正在设置对象的状态 setDetails(obj) { this.setState({ types: obj, currentStep: 2 //Next step Details }); } render() { <ListComponent types={this.setDetails.bind(this)} /> } setDetails(obj){ 这是我的国家({ 类型:obj, 当前步骤:2//

我有一个父组件a,它正在设置对象的状态

setDetails(obj) {
  this.setState({
      types: obj,
      currentStep: 2 //Next step Details
    });
  }

render() {
 <ListComponent
     types={this.setDetails.bind(this)}
 />
}
setDetails(obj){
这是我的国家({
类型:obj,
当前步骤:2//下一步详细信息
});
}
render(){
}
列表组件

render(){
}
详细信息组件 我正在设置将调用父组件并设置类型值的类型值。现在我想访问列表组件中的类型。
有办法吗

您可以分别将
state
方法传递给
A
(父项)的
ListComponent
,如下所示:

Parent A
setDetails = (obj) => {
  this.setState({
      types: obj,
      currentStep: 2 //Next step Details
    });
  }

render() {
 <ListComponent
     setType={this.setDetails}
     types={this.state.types}
 />
}

List Component
render() {    
    <DetailsComponent
       setType={this.props.setType}
       types={this.props.types}
     />
    }
父级A
setDetails=(obj)=>{
这是我的国家({
类型:obj,
当前步骤:2//下一步详细信息
});
}
render(){
}
列表组件
render(){
}
现在,在子组件(
ListComponent
)中,您可以调用
this.props.setType()
来设置父组件中的状态。在父组件中设置状态后,您将通过
this.props.types
父组件A在子组件中获取这些值

setDetails(obj) {
  this.setState({
      types: obj,
      currentStep: 2 //Next step Details
    });
  }

render() {
 <ListComponent
     callback={this.setDetails.bind(this)}
     types={this.state.types}
 />
}
setDetails(obj){
这是我的国家({
类型:obj,
当前步骤:2//下一步详细信息
});
}
render(){
}
列表组件

render() {    
    <DetailsComponent
       callback={this.props.callback}
       types={this.props.types}
     />
 }
render(){
}

在详细信息组件中,使用
this.props.callback
设置类型的值。更新类型后,将调用渲染,您将获得列表组件中类型的更新数据,如
this.props.types

哪个是
列表组件的父组件
render() {    
    <DetailsComponent
       callback={this.props.callback}
       types={this.props.types}
     />
 }