Why Redux

作者 Zhe Wang 日期 2018-01-12
web
Why Redux

Desipte having been implemented in codes, Redux should be regarded as a design pattern rather than a framework. People rarely use Redux individually.

A pure JavaScript implementation, as Redux announced itself, has nothing to do with React indeed, but it would be given a pretty low likelihood to be as prevalent as now without React. Truly, Redux works especially well with React.

It is easy to follow tide without thinking painstakingly, as if it is easy to put Redux in use without knowing why.

So, why?

Some flaws exist in React. State oriented is never an issue, neither is modularization. State management in a modularized application is an issue. React intended to confine state management to component, which is ideal but not realistic. Interactions among components become easily obstructed as structural complexity grows up.

The main point of Redux is simple. An application can be erected in one-way data flow, which means the whole application shares only one states tree regardless of the number of files or components. See, obstacles in interactions among components are easily tackled. And that’s why there is a Store to store states tree in Redux.

Also, even a tiny update will trigger rendering method on the top though most of DOMs won’t be re-rendered because of diff algorithm. You will probably fall into contemplation again, feeling puzzled about why rendering method cannot be triggered just on the exact component since its antecedent components’ states remain unchanged. The truth is, the setState method may be a bit odd that it allocates a new object for new states rather than tampers datas on the same reference. Therefore, invoking setState method halfway will result in loss of partial states of Store since it has no idea where to locate new references.

Thus Redux brings in concepts like Reducer and Action. Dispatch an Action when you need to change some states and Reducer will make Store pour states data from top as soon as it’s notified. The core designing thought of Redux comes in naturally and concisely as you would sense it.

It might be hard to assert that Redux is born for React since it is capabable to run individually. The reason why Redux keeps insisting on that it is not related to React is possibly that Redux doesn’t want to be treated as third-party library for React, which is taken over by React-Redux now.