Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/22.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_Api_Components_State - Fatal编程技术网

Javascript 反应没有子组件的父组件

Javascript 反应没有子组件的父组件,javascript,reactjs,api,components,state,Javascript,Reactjs,Api,Components,State,例如,我有5个组件,第一个组件作为父组件的其余部分作为子组件,来自API的所有数据/响应是否都存储在父组件中,然后传递给子组件?或者每个子组件都有一个状态来保存来自API的数据/响应? 反之亦然?所以每个组件都有自己的状态 export class Home extends Component { constructor(props){ super(props) this.state = { dataComponentChild1: [], dataC

例如,我有5个组件,第一个组件作为父组件的其余部分作为子组件,来自API的所有数据/响应是否都存储在父组件中,然后传递给子组件?或者每个子组件都有一个状态来保存来自API的数据/响应? 反之亦然?所以每个组件都有自己的状态

export class Home extends Component {
  constructor(props){
    super(props)
    this.state = {
      dataComponentChild1: [],
      dataComponentChild2: [],
      dataComponentChild3: [],
      dataComponentChild4: [],
    }
  }

  async componentDidMount(){
    await Api.post('/example1-endpoint')
    await Api.post('/example2-endpoint')
    await Api.post('/example3-endpoint')
    await Api.post('/example4-endpoint')
    .then((response) => {
      const responseJson = response
      if (responseJson.data.STATUS_CODE === '200') {
        this.setState({
          dataComponentChild1: responseJson.data,
          dataComponentChild2: responseJson.data,
          dataComponentChild3: responseJson.data,
          dataComponentChild4: responseJson.data,
        });
      }
    })
  }

  render() {

    const {dataComponentChild1,dataComponentChild2,dataComponentChild3,dataComponentChild4} = this.state

    return (
      <React.Fragment>
        <Component1 props1 = {dataComponentChild1} />
        <Component2 props2 = {dataComponentChild2} />
        <Component3 props3 = {dataComponentChild3} />
        <Component4 props4 = {dataComponentChild4}/>
      </React.Fragment>
    )
  }
导出类主扩展组件{
建造师(道具){
超级(道具)
此.state={
dataComponentChild1:[],
dataComponentChild2:[],
dataComponentChild3:[],
dataComponentChild4:[],
}
}
异步组件didmount(){
等待Api.post(“/example1 endpoint”)
等待Api.post(“/example2 endpoint”)
等待Api.post(“/example3端点”)
等待Api.post(“/example4 endpoint”)
。然后((响应)=>{
const responseJson=响应
如果(responseJson.data.STATUS_CODE==='200'){
这是我的国家({
dataComponentChild1:responseJson.data,
dataComponentChild2:responseJson.data,
dataComponentChild3:responseJson.data,
dataComponentChild4:responseJson.data,
});
}
})
}
render(){
const{dataComponentChild1,dataComponentChild2,dataComponentChild3,dataComponentChild4}=this.state
返回(
)
}
如果我想同时发布/访问多个端点,该怎么办?
上述方法正确吗?

如果每个API的响应仅在单个子组件中使用,则最好从每个子组件的
componentDidMount
方法调用相关API调用,并将结果存储在子组件自身的状态中。此外,您不能使用
然后使用
异步等待捕获
调用。请阅读基于
promise
的异步语法和
async wait
语法之间的区别。如果您的组件状态是独占的,您可以选择将每个组件的状态保存为其自己的状态,此外,您可以只编写单个组件并只传递更改的数据(例如端点)并在公共组件“componentDidMount”中调用该端点。通过这种方法可以删除大量重复代码。