Moreover, by using redux instead of the useReducer hook you can take advantage of really efficient devtools and middlewares. You already have everything you need to manipulate a complex state. However, if you use redux in your application, it seems quite strange to have to use useReducer. We’re really very very close to a redux state that we can associate to a component, it’s great. The useReducer hook allows to set a state that can be modified through actions. Since the arrival of hooks, we have many more tools provided directly by React to manage the state of our components. What I want to do is have a selector function in my Redux file, something like this export const selectorFunction ( state ,key) > state key where key would be m2024, for example. Don’t use useReducer for your business data It is set by default as unknown so I get an error when I try to return state.SLICENAME ( Error: Object is of type 'unknown' ). You cannot access a new value from a hook within a callback after something that would result in a state change (such as a useState setter or a useDispatch dispatch). React-Redux useSelector typescript type for state Ask Question 52 I'm using the useSelector (state > state.SLICENAME) hook from React-Redux however I'm having difficulty defining the state parameter. This information is then calculated once per action and not every time it is rendered. Actually, looking at your code snippet, it seems like youre confused on how both hooks, React, and React-Redux work. In Bedrock, we use a folder named modules which groups together the different folders associated with the feature of your application. Some very good articles explain it notably through the use of DDD principles. In my approach, we would tend to represent the business of the application more than the technical stuff implied. What we recommend at Bedrock is a functional splitting of the application. This feature of redux allows to manipulate not one but several reducers which act respectively on the state. In the case of a small application, this is not a problem.įor applications expressing a complex and evolving business, it is better to opt in for the combineReducers solution. One might be tempted to manipulate only one reducer. The reducer is the function that is in charge of building a new state at each action. Note that if you use the Redux Toolkit, some of the tips/practices presented in this article are already integrated directly into the API. You could also take a look at Redux offical style guide in which you could find some of those tips and others. I recommend this documentation if you want to see how to implement it in your applications. This article is not an introduction to React or Redux. So here are my tips for maintaining and using React and Redux in your application without going mad. The Bedrock team (to which I currently belong) kept the technical stack of the project up to date to take advantage of the new features of react, react-redux and redux. After 2 years using React with Redux for the video platform 6play, I was able to identify good practices and pitfalls to avoid at all costs.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |