菜鸟教程小白 发表于 2022-12-12 17:55:11

ios - NavigatorIOS 下不调用 React-native 组件的 componentDidMount()


                                            <p><p>我用 <code>ListView</code> 在结构下创建了一个组件:<code>TabBarIOS</code> > <code>NavigatorIOS</code> > <code>ListView</code>。我尝试在 <code>componentDidMount()</code> 中获取数据。但除非我在 <code>componentWillMount()</code> 中获取它,否则它不起作用。为什么?</p>

<p>我把我的工作放在这里 <a href="https://github.com/chiakie/MyAwesomeProject" rel="noreferrer noopener nofollow">https://github.com/chiakie/MyAwesomeProject</a> <br/>
在 <code>MotorcycleList.js</code> 中,<code>componentDidMount()</code> 似乎永远不会被调用。</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p><code>componentWillMount()</code> 生命周期事件实际上是调用 <code>fetchData()</code> 的正确位置,因为它在组件挂载之前<em>之前</em>被调用, 这样你就可以 setState 做到数据在挂载时就在那里。</p>
<blockquote>
<h2><a href="https://facebook.github.io/react/docs/component-specs.html#mounting-componentwillmount" rel="noreferrer noopener nofollow">Mounting: componentWillMount</a></h2>
<p>void componentWillMount()</p>
<p>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.</p>
</blockquote>
<p>而 <code>componentDidMount()</code> 在组件已经挂载后呈现。</p>
<blockquote>
<h2><a href="https://facebook.github.io/react/docs/component-specs.html#mounting-componentdidmount" rel="noreferrer noopener nofollow">Mounting: componentDidMount</a></h2>
<p>void componentDidMount()</p>
<p>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.</p>
</blockquote></p>
                                   
                                                <p style="font-size: 20px;">关于ios - NavigatorIOS 下不调用 React-native 组件的 componentDidMount(),我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/34393109/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/34393109/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - NavigatorIOS 下不调用 React-native 组件的 componentDidMount()