简体   繁体   English

两次响应本机渲染

[英]React Native rendering twice

When call state from AsyncStorage in new page. 在新页面中从AsyncStorage调用状态时。 I found error and then i tried to console.log the state in render function i found console.log twice, the first console was null and the second was state from AsyncStorage . 我发现错误,然后尝试在呈现函数中使用console.log两次发现console.log的状态,第一个控制台为null,第二个为AsyncStorage的状态。 Why it happen? 为什么会发生? How to solve this problem? 如何解决这个问题呢?

React will call render as many times as it requires to reflect changes on the state object, if you are asynchronously fetching data and updating the state, you will see a console.log message as the component is first rendered, and once again once the data is fetched and the state updated. React将根据需要多次调用render来反映状态对象上的更改,如果您异步获取数据并更新状态,则将在第一次渲染组件时看到console.log消息,然后再次显示该数据获取并更新状态。

In other words, working as expected, if you want to only log whatever message just once, try moving this line to the component constructor or componentDidMount methods. 换句话说,按预期工作,如果您只想记录任何消息一次,请尝试将此行移至组件构造函数或componentDidMount方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM