In the last days I have searched for a documentation on how and when is it best to use redux and how to design the state tree.
My research ended in the conclusion that there is no one correct answer. Then I came across this answer in github on that manner by petehunt (The answer is intended for flux). He's answer is almost unambiguous:
I think the use of flux is justified when two conditions are met:
- You have a piece of data that needs to be used in multiple places in your app, and passing it via props makes your components break the single-responsibility principle (i.e. makes their interface make less sense)
- There are multiple independent actors (generally, the server and the end-user) that may mutate that data.
My poor knowledge on the subject gave me hard time to understand why the first condition is related to the question.
Please share your opinion about this and if it is possible, relate your answer to the following example:
A simple app that has a login component and show-profile-details component. My app will talk to a server for the login functionality.
The simplest way to design the state tree would be by:
{
connectedUser: {
connectedUserID:String (There could be much more details here)
}
serverStatus: String (connected/connecting/disconnected),
languageID: String
}
The problem is that every design will break the single source of truth. For example, one could store the status of the login (fail,loading,success),the number of login tries,the profile being displayed right now. The point is that this list will never be over and in one way or another, some data will be stored in the component!
How could one know what is best to put in the state tree and what to keep in the login component and export it to other components?
Some extra details and requests:
- I'm developing in Angular2 (There is no tag for Angular2)
- Please share your sources of your answers if they are exists.
Thanks!