Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

designing redux state tree

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?

like image 885
JonH Avatar asked May 17 '16 23:05

JonH


People also ask

What is state tree in Redux?

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.

What design pattern is Redux?

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.

What are 3 main concepts of Redux?

In this brief introduction to Redux, we'll go over the main concepts: reducers , actions , action creators and store .


1 Answers

There is a lot of differing opinion on this. Here is what I do.

  1. 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

  2. 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.

like image 69
Austio Avatar answered Sep 23 '22 07:09

Austio