微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在ReactJS中通过AJAX加载初始数据的位置

我正在使用React-Redux来构建应用程序.要加载React智能组件的初始数据,我需要调度一个Redux操作,在该操作中将发生Ajax调用.我已经尝试在构造函数(ES6实现),componentwillMount和componentsnetDidMount中调度操作.他们都工作了.我的问题是:React智能组件中是否存在应该调度操作的推荐位置.

解决方法

编辑: Dan Abramov recently stated

In future versions of React we expect that componentwillMount will fire more than once in some cases,so you should use componentDidMount for network requests.

在componentDidMount中

阅读here.

Fetch data in componentDidMount. When the response arrives,store the data in state,triggering a render to update your UI.

When fetching data asynchronously,use componentwillUnmount to cancel any outstanding requests before the component is unmounted.

关于“为什么在componentDidMount中”的文档非常缺乏.我相信如果您使用服务器端渲染,则不会调用componentwillMount,因此这可能是首选componentDidMount的原因.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐