ios React-native 组件的 componentDidMount() 在 NavigatorIOS 下没有被调用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34393109/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
React-native component's componentDidMount() is not called under NavigatorIOS
提问by Chiakie
I create a component with ListView
under the structure: TabBarIOS
> NavigatorIOS
> ListView
. I try to fetch data in componentDidMount()
. But it didn't work unless I fetch it in componentWillMount()
. Why?
我ListView
在结构下创建了一个组件:TabBarIOS
> NavigatorIOS
> ListView
。我尝试在componentDidMount()
. 但它没有工作,除非我在componentWillMount()
. 为什么?
I've put my work here https://github.com/chiakie/MyAwesomeProject
In MotorcycleList.js
, componentDidMount()
seem to never be called.
我已经把我在这里的工作https://github.com/chiakie/MyAwesomeProject
在MotorcycleList.js
,componentDidMount()
似乎永远不会被调用。
采纳答案by Chris Geirman
The componentWillMount()
life cycle event is actually the correct place to call fetchData()
because it is invoked once beforethe component mounts, this way you can setState do that the data is there when it mounts.
在componentWillMount()
生命周期事件实际上是调用正确的位置fetchData()
,因为它被调用一次之前的组件支架,这样你就可以做的setState该数据是有它的坐骑时。
Mounting: componentWillMount
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.
安装:componentWillMount
void componentWillMount()
在客户端和服务器上调用一次,紧接在初始呈现发生之前。如果您在此方法中调用?setState?,render()? 将看到更新的状态,并且尽管状态发生变化也只会执行一次。
Whereas componentDidMount()
renders after the component had already mounted.
而componentDidMount()
在组件已经安装之后渲染。
Mounting: 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.
安装:componentDidMount
void componentDidMount()
仅在客户端(不在服务器)上调用一次,在初始渲染发生后立即调用。在生命周期的这一点上,您可以访问任何对您的孩子的引用(例如,访问底层 DOM 表示)。子组件的?componentDidMount()?方法先于父组件调用。