I'm currently in the process of learning Redux, and I've more or less got the hang of the basic concepts. I understand how to work with actions and reducers and all that. What I am struggling with is understanding how to properly design a state tree. I get caught up on the particulars of what should/shouldn't be stored in the application state, when is it acceptable to use component state, the best way to handle state changes, etc.
Are there any good tutorials or blogs out there anyone can recommend for understanding the best practices of designing state?
State (also called the state tree) is a broad term, but in the Redux API it usually refers to the single state value that is managed by the store and returned by getState() . It represents the entire state of a Redux application, which is often a deeply nested object.
At its core, Redux is really a fairly simple design pattern: all your "write" logic goes into a single function, and the only way to run that logic is to give Redux a plain object that describes something that has happened.
In this brief introduction to Redux, we'll go over the main concepts: reducers , actions , action creators and store .
There is a lot of differing opinion on this. Here is what I do.
Reducers - hold things that deal with my data model (normally anything that is in a database), that needs to be saved and used later or used across components
LocalState (react setState) - deals ui elements in a single component related to user input and interaction
So if i were modeling a response to this question. Redux store would have
store = {
session: { token: 'randomUid' }
user: { name: 'Austio' }
question: { id: 37288070 }
}
When I select the text box to enter values in to create an answer to this question is would handleInput from this box, which would setState of the answerText.
When is handleSubmit for the form, i would dispatch out based on success something like NEW_ANSWER with the questionId and the answer so that i could store it in the store to be used whereever i need it.
My best advice is to just start programming stuff, it is very difficult to find the edges of your preferences with using redux/react without that.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With