Jeroen Bourgois
Jeroen Bourgois

Reputation: 180

Redux store general data, non persistent

I have a very general question but I cannot find a direction for a good solution, not on Google, stackoverflow...

I have a couple of reducers in my app reflecting my state tree. A 'user' reducer and a 'survey' reducer (making a survey app). But now I want to create a profile page for the user. The answers to all the available fields will off course go into the 'user' store, but what about list options? For instance, selecting your gender (m/f) or selecting your favorite past time... Somehow I feel these options should not reside in the user store, or am I wrong? I feel like for a given user I want to have a state that says 'gender: m', but in the app I want to show 'Male' instead of 'm'. Or am I making a big fuss out of nothing and should I just keep one big key/value list of all these label/value things an access it that way?

Upvotes: 0

Views: 216

Answers (1)

Rob Wise
Rob Wise

Reputation: 5130

If they are simple attributes such as gender, I agree with @kujira that you can make the underlying values whatever you want (such as m for male) and then display them a different way in your JSX if that's really what you want to do.

As far as whether user attributes should go inside the Users reducer, the answer is probably yes. Just because the model may have many attributes doesn't mean you need to create another reducer. Generally you want to keep the your redux state normalized as much as possible. Normalization says that one-to-one associations go in the same table (or in this case, reducer state).

The only case where you might have a different reducer is when you essentially have a separate model and the two are associated with each other in a one-to-many or a many-to-many relationship. If it's a one-to-one relationship, or you are just wanting to constrain an attribute to certain values, then the rules of normalization say to keep it on the same model.

If you are worried about how to enforce constraining, you could add a check in your reducer that blows up your app if a value other than m or f is passed for gender, but that's sort of practicing overly defensive programming. If you really feel you need to go that route, an option to consider is Facebook flow annotations.

Upvotes: 1

Related Questions