在 componentDidMount (react/redux) 上分派一个动作 [英] dispatch an action on componentDidMount (react/redux)
问题描述
我是 React/redux 的新手.因为我想问一个(也许是哲学上的)问题.
I am relativity new to react/redux. There for I want to ask a (perhaps a philosophic) question.
可以在 React 组件的 componentDidMount
上分派一个动作(例如触发 api 调用)吗?
Is it ok to to dispatch an action (e.g. to trigger an api-call) on componentDidMount
of a react component?
如果不是,我为什么以及在哪里发送动作?
If not, why and where should I dispatch the action?
如果是,那么就没有其他问题了吗?:)
If yes, then no further questions? :)
推荐答案
是的,在 componentDidMount()
上分派一个动作是可以的,即使是推荐的做法,因为它不会减慢初始 UI 渲染.
Yes, dispatching an action on componentDidMount()
is OK, and even the recommended thing to do since it will not slow down the initial UI render.
由于该函数在组件初始呈现后运行,请记住,在呈现组件和从 api 调用接收数据之间可能会有一段时间.
Since the function runs after the component has initially rendered, keep in mind that you may have sometime between the moment the component is rendered, and the moment you receive the data from the api call.
这篇关于在 componentDidMount (react/redux) 上分派一个动作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!