我用 ListView 在结构下创建了一个组件:TabBarIOS > NavigatorIOS > ListView 。我尝试在 componentDidMount() 中获取数据。但除非我在 componentWillMount() 中获取它,否则它不起作用。为什么?
我把我的工作放在这里 https://github.com/chiakie/MyAwesomeProject
在 MotorcycleList.js 中,componentDidMount() 似乎永远不会被调用。
Best Answer-推荐答案 strong>
componentWillMount() 生命周期事件实际上是调用 fetchData() 的正确位置,因为它在组件挂载之前之前被调用, 这样你就可以 setState 做到数据在挂载时就在那里。
void componentWillMount()
Invoked once, both on the client and server, immediately before the initial rendering occurs. If you call setState within this method,render() will see the updated state and will be executed only once despite the state change.
而 componentDidMount() 在组件已经挂载后呈现。
void componentDidMount()
Invoked once, only on the client (not on the server), immediately after the initial rendering occurs. At this point in the lifecycle, you can access any refs to your children (e.g., to access the underlying DOM representation). The componentDidMount() method of child components is invoked before that of parent components.
关于ios - NavigatorIOS 下不调用 React-native 组件的 componentDidMount(),我们在Stack Overflow上找到一个类似的问题:
https://stackoverflow.com/questions/34393109/
|