gpt4 book ai didi

javascript - 在哪里放置用于在 React 应用程序中加载初始服务器数据的逻辑?

转载 作者:数据小太阳 更新时间:2023-10-29 05:11:49 25 4
gpt4 key购买 nike

我正在使用 React-Redux 构建应用程序。

要为 React 智能组件加载初始数据,我需要在将发生服务器数据请求的地方分派(dispatch)一个 Redux 操作。

我已经尝试在构造函数(ES6 实现)、componentWillMountcomponenetDidMount 中分派(dispatch)操作。他们都工作了。


我的问题是:

在 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 的原因。

关于javascript - 在哪里放置用于在 React 应用程序中加载初始服务器数据的逻辑?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39620572/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com