Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/26.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 通过react路由器传递道具在componentDidMount中不可用?_Reactjs_Asynchronous_React Router - Fatal编程技术网

Reactjs 通过react路由器传递道具在componentDidMount中不可用?

Reactjs 通过react路由器传递道具在componentDidMount中不可用?,reactjs,asynchronous,react-router,Reactjs,Asynchronous,React Router,我已经通过react路由器将道具传递给了上述组件: <BrowserRouter> <Switch> <Route path="/product-page" exact render={(props) => ( <ShopPages {...props} response={this.state.response}/>)}/> </Switch> </BrowserRout

我已经通过react路由器将道具传递给了上述组件:

<BrowserRouter>
      <Switch>
          <Route path="/product-page" exact render={(props) => ( <ShopPages {...props} response={this.state.response}/>)}/>
      </Switch>
    </BrowserRouter>
prop
this.props.response
可在组件I can console.log in The render()中使用。我不确定为什么在上面的场景中,控制台显示阵列为空。我已尝试查看数据是否可用,然后将数据显示为so
console.log('show',this.props.response&&this.props.response)
或通过添加
async
来显示:

async componentDidMount() {
const data = await this.props.response
}

但这也没什么作用。有什么帮助吗?

我只是假设
this.state.response
不是承诺,但实际上是数据,因为您说它是一个空数组,因此async/await将不起任何作用

ComponentDidMount()仅在组件装载时触发一次,而不是根据装载后可能发生的道具更新执行操作的地方。因此,我建议通过承诺,如果这不是一个选项,那么做类似的事情

componentDidUpdate(prevProps, prevState) {
    if (!prevProps.response.length && this.props.response.length) {
      //Your code
    }
  }
移除渲染它的
组件周围的
()
,查看它是否更改????
componentDidUpdate(prevProps, prevState) {
    if (!prevProps.response.length && this.props.response.length) {
      //Your code
    }
  }