Friday, February 5, 2021

State management in React Native Part-1

It is best to use React's built-in state management capabilities rather than external global state for smaller Apps, but default React state has certain limitations: Component state can only be shared by pushing it up to the common ancestor, but this might include a huge tree that then needs to re-render.

Context api with React Hooks

The React Context API is React’s approach of managing state in more than one elements that are not directly connected. You can create your individual custom hooks to avoid repetition and make code cleaner. Context supplies a way to pass data in the course of the component tree without having to pass props down manually at every level. Custom hooks are customary JavaScript functions which is able to use different React hooks.

Without Hooks, the Context API may no longer seem like much when compared to Redux, however mixed with the useReducer Hook, we have now an answer that finally solves the state management drawback.
Custom hooks resolve the problem of fending off repetition and make code cleaner. Context supplies a strategy to move information in the course of the component tree with no need to go props down manually at each and every stage.

React Hooks are a kind of serve as that allows the execution of customized code in a base code. In React, Hooks are special functions that permit us to “hook into” its core features.
With the useContext API and React Hooks, there is not any need to install external libraries or add a bunch of records data and folders in an effort to get our app running. This makes it a miles more effective, easier technique to take care of global state management in React applications.

Pros Of Context API

  • Scalable from any vary of application (small, mid, large)
  • Comparatively much less complex than Redux
  • Implementation price is decrease
  • No need to pass data to every level. Consumer element circumstances can get admission to the entire knowledge provided by means of the Provider Component at any stage.
  • The core part of React JS library so no need to import any further libraries thus dependencies is reduced.
  • Code is easy to handle and really reusable.
  • Integration of React’s modules is seamless
  • Context is simpler to use and is a great way of passing state down to any level of a component tree without having to pass props down through traversal.
  • When combined with React Hooks,Context API solves issues we have got a state management solution that is less time-consuming to set up, and requires minimal code.

Cons Of Context API

  • Libraries corresponding to Redux-form which has excellent integration with React + Redux. So Redux must be built-in to use redux-form.
  • Middlewares needs to be written as customized code. E.g. APIs,custom purposes
  • Somewhat more difficult to debug for builders.
  • unless you separate your different states into separate Providers, the consumers of a provider will rerender if the context of a Provider changes. This will also be very inefficient in a large app the place the are many states you use because the context in a provider or if only a few states change that effect the renderings of other components.
  • The cons of using Context is that unless you separate your different states into separate Providers, the consumers of a provider will rerender if the context of a Provider changes. This can be very inefficient in a large app where there are many states you use as the context in a provider or if only a few states change that affect the renderings of other components.
  • The cons of the usage of Context is that until you separate your different states into separate Providers, the consumers of a supplier will rerender if the context of a Provider changes. This can be very inefficient in a large app where there are lots of states you employ because the context in a provider or if only a few states exchange that have an effect on the renderings of alternative parts.

to be continued... 

No comments:

Post a Comment