Reputation: 301
From the redux documentation, "Redux can be described in three fundamental principles: Single source of truth, State is read-only, Changes are made with pure functions."
As someone relatively new to React, and in the process of assessing the need to learn Redux, I am confused because these principles seem to either already exist in React, or be implementable in React by simply adopting conventions. Is Redux mostly about writing code a certain way, or does it do something I don't understand yet?
How is this different from the React concept of managing state in the component that is the highest common owner (or higher)? Would you achieve a "single source of truth" if you designate a single component in your whole app for managing state? Would that produce pitfalls that are somehow avoided when I install Redux?
React state is already immutable, so it sounds like this principle is already fully captured by React. I see the Redux documentation mentions "subtle race conditions" but I don't see that mentioned in the React documentation. When would such race conditions become a problem? Could I avoid them without Redux?
Redux is very specific about how they alter state: using pure function reducers with a single, flexible function prototype. The benefits are stated as "you can control the order in which they are called, pass additional data, or even make reusable reducers..." I'm admittedly unclear on how call-order would get controlled in just React, but it seems like the other benefits (passing additional data, and reusable reducers) would come from just adopting the conventions:
changeState( state, action )
where action looks like:
{type: "action identifier", data: <data needed to complete action> }
and changeState() contains a switch based on action.type, doesn't produce side-effect, and depends only on the passed parameters.
Upvotes: 0
Views: 80
Reputation: 4987
The ContextApi
and Hooks
are what you need. There is actions, reducers, and context is immutable.
Note that Redux exists because React ContextApi
was unstable until react 16.8 (If i remember well). Now that it is fully operational, go for it. Hooks will give you the tools you need to handle it easily.
I always refer to this article to setup my context because it is the easiest way to handle it I think, but you should read the documentation before.
Upvotes: 1