[英]React-Redux store or Passing the api in props to the components which is better way
I am just going through a confusion so far.到目前为止,我只是在经历一个混乱。 I am developing a React application for that some amount of API call is required.
我正在开发一个需要一定数量的 API 调用的 React 应用程序。 so I just confused with whether calling the API in the component and passing the dats by props to the required components or Using redux thunk to fetch the api's and store in to a React store and fetching the required data by dispatching the action when ever it required.
所以我只是混淆了是否在组件中调用 API 并通过 props 将 dats 传递给所需的组件,或者使用 redux thunk 来获取 api 并存储到 React 存储中并通过在需要时分派动作来获取所需的数据.
Suggestions will be appreciable :)建议将是可观的:)
I strongly recommend not to use React-Redux.我强烈建议不要使用 React-Redux。 Here are the reasons
以下是原因
All in all, your React can manage all your state communication problems alone.总而言之,您的 React 可以单独管理您所有的状态通信问题。 You don't need another library for state management.
您不需要另一个用于状态管理的库。
Note: Whenever I say React, I mean React newer versions.注意:每当我说 React 时,我的意思是 React 的较新版本。 React 16.8+
反应 16.8+
Calling the API is surely gonna be done in the component.调用 API 肯定会在组件中完成。 Like so:
像这样:
const Component = () => {
useEffect(() => {
const fn = async () => {
await apiCall()
}
fn()
}, [])
}
But your question resides on where you should store it.但是你的问题在于你应该在哪里存储它。
This is simple:这很简单:
redux-persist
( https://github.com/rt2zz/redux-persist ) so it's not only to have a global object you can access.redux-persist
( https://github.com/rt2zz/redux-persist ) 的数据持久性,因此它不仅具有您可以访问的全局对象。 Features:特征:
a.一种。 Persistance b.
持久性 B. Global state accesible anywere c.
可访问的全局状态 c. Enhanced debugging with Redux dev tools
使用 Redux 开发工具增强调试
prop drilling
then React.Context
is a good optionprop drilling
那么React.Context
是一个不错的选择drilling
2 or 3 leves with your props, just don't overkill it using a Context
drilling
2 或 3 层,请不要使用Context
过度使用它
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.