I understood React lifting-state-up "https://reactjs.org/docs/lifting-state-up.html". By this method a developer needs to pass the state functions in parents as props to child. After which I did a couple of session on redux. But, I am unable to understand the main differences in lifting state up vs Redux, flux state management in simple comparisons.
Flux is unopinionated about mutating data, but Redux doesn't like mutations and many packages complementary to Redux assume you never mutate the state. You can enforce this with dev-only packages like redux-immutable-state-invariant, use Immutable.
Redux manages state and state transformations and is often used with React, but React has its own concept of state. When using these libraries, it's good to know which to use when. Even if you choose to use Redux in your project, you will still need to make decisions on how much of your data is stored in Redux.
In React, sharing state is accomplished by moving it up to the closest common ancestor of the components that need it. This is called “lifting state up”.
What is state management in Redux? State management is essentially a way to facilitate communication and sharing of data across components. It creates a tangible data structure to represent the state of your app that you can read from and write to.
Does it mean inside a big web applications "lifting state up" should not be used and redux/flux becomes inevitable
Absolutely not, you need to be use redux/flux, only when the data being used by your app is shared between a large number of components who do not share a close enough parent and that the data keeps on changing and the view needs to be refreshed.
In case when the data is being shared in a small section of your App, you might consider using Context-API
for this purpose too.
Redux simplifies the whole process of state management, but is there any advantage other than abstraction and simplification.
Yes, redux offers a wide range of advantages
Persist state to a local storage and then boot up from it, out of the box.
Pre-fill state on the server, send it to the client in HTML, and boot up from it, out of the box.
Serialize user actions and attach them, together with a state snapshot, to automated bug reports, so that the product developers can replay them to reproduce the errors.
Pass action objects over the network to implement collaborative environments without dramatic changes to how the code is written.
By using Redux we will have a single source of truth as compared to "Lifting state up" where we need to keep state in multiple components and thus adding complexity to overall code. In lifting-state-up you can have a single source of truth the only shortcoming of that approach would be your parent component would be cluttered with many states.
As I said earlier, if the data if shared between components that do not have very direct common ancestor, you might prefer Redux. Redux also offers an advantage that it implements a connect as a PureComponent
and hence state updates only affect the components that are using them and will not cause a re-render for other components which don't use those particular states as props.
With Lifting state up, you need to pass down the values all the way down though each and every component and then you need to make sure that the intermediate components are Pure.
Is there a performance angle between redux and lifting state up
The performance issue comes into picture when you are passing state down and don't use PureComponents, causing unnecessary re-renders. Redux is helpful here
Thus in my opinion you must first try to develop a react application (of medium complexity) without Redux. Use the "Lifting state up" concept. At some point in the course of the development you would intuitively realize that having the state management library like Redux would make life much easier. Sometimes it's better to learn it the hard way. Moving to Redux when it's absolutely necessary will make you appreciate it more.
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