Sunday, February 7, 2021

State management in React Native Part-2

Context can only store a single value, not an indefinite set of values each with its own consumers. 

In order to solve this problem, we needed a way to provide a global state that all components, regardless of how deeply nested they are, could access. React Hooks and Redux are ways to manage state in a React app.

In Previous blog we've got mentioned about Context API, now will see in information about the Redux.

Redux:

The Redux describes it as a predictable state container for JavaScript applications that helps us to write programs that behave constantly, run in several depths and widths of elements, and are easy to check.

Redux is a state control library used to store and manage the state of a react software in a centralized position. Redux abstracts all states from the app into one globalized state object in order that any component and any a part of the app can get access to the different properties of the global state. Redux is separate from React and there are libraries to combine Redux into React apps.


 To a good extent, Redux works for state management in React applications and has a couple of advantages,however extra code needed to get it working in our application introduces a large number of complexity on code for learners.

Redux requires three main building parts to function: actions, reducers, and store. 

Actions

These are objects that are used to send data to the Redux store.
They generally have two properties: a type property for describing what the action does and a payload property that incorporates the information that should be modified in the app state.

Reducers

Reducers describe how your actions transform from state to state and keeps track of the actions you are switching from.
These are pure functions that implement the action behavior. They take the current application state, perform an action, and then return a new state.

The store

The Store is the object that brings the actions and reducers together.
The store is where the application’s state is saved. There is only one store in any Redux application.
The state management library Redux used to be created to handle this particular problem. It supplies a JavaScript object known as the store, which, as soon as arrange, includes all states in your application and updates them when necessary. Here is a simplified visualization of ways Redux works.




Image above shows how a single event on View(Screen) works internally with Redux.

Pros of Redux

  • Business logic is totally separated from the UI
  • The code is straightforward to deal with and very reusable.
  • Heaven for mid to large scale applications.
  • Have its personal debuggable tool to manage state.
  • Redux-form is far better than the normal method of doing form-related stuff.
  • Redux Middlewares can make your process easy.
  •  Redux attempts to make state mutations predictable by way of imposing positive restrictions on how and when updates can happen and are nice for applications that experience a couple of states that can achieve different components in an element tree. Redux turns out to be useful if in case you have an app that has a extra complex state that is updating frequently. The primary benefit is if states have now not changed they will not re-render.
  • Redux solves state management problems, it is truly time-consuming to make use of, and introduces an entire new layer of complexity to our application in a great way.

Cons of Redux

  • Not beneficial for small-scale applications
  • Complex code structures and high implementation cost to add a small feature
  • Needs to be injected as a dependency
  • In order to make a large component reusable, the data needs to be passed at each and every stage of the hierarchy of the tree which is bulky but this will also be resolved with the context API integration together with Redux.

If you could have used i18n for language translation, redux state really helpful for underneath reasons.

  • This will can help you pass the initial value from the database, local file
  • When the person changes the language you'll be able to alternate the whole application language without even refreshing the UI.
  • When the person adjustments the language this will also allow you to retrieve the new language from API, local file or even from constants
  • You too can also save different necessary issues with the strings such as timezone, currency, direction (RTL/LTR) and list of available languages
  • You can define the change language as a typical redux action


Please comment if you want to know any other topic.


Redux brought back More (Definitions, Synonyms, Translation)

No comments:

Post a Comment