React Hooks vs. Redux
Tags: react, javascript, redux, hook
Published: 2022-12-29 10:29:15

In developing and application, Data management is an important concept used by developers to minimize potential errors by establishing processes and policies for usage and building a single source of truth for the data being used to make decision across the app.
In in this article, I will be discussing with you React Hooks and Redux and how they differ.

> It is important to note that Redux and React Hooks should not be seen as the opposite of each other. They are different things, with distinct goals.

![yeeeeesssssssss](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6hurule5muepbl0p4por.gif)



## React State Management 
As your application grows, it is imperative to be conscious of how your app state is organized and how the data flows between your components. Redundant or duplicate state does not help give a single source of truth to your decision making data, which is a common source of bugs.

To handle data between separate components in React, developers use a practice known as `prop drilling`.
This is the act of passing data from a top level component till it get's to the child component you want to access it from.
Not only does this method requires writing extra tones of code, It also affects the architectural design of the app and also makes it difficult to debug when you encounter errors.

![How](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b1gmls9qq2bp7t3wim7w.gif)


To curb this menace, Redux is used, An open source JavaScript library for managing application state.

## React Hooks vs. Redux
Redux has been the go-to solution to developers when it comes to state management. To some extent, it works well for state management in React apps. However, because of its verbosity, it is a bit challenging to master, and the additional code required to make it function can add a lot of needless complexity.

On the other hand, installing other libraries or adding several files and folders is not recommended in app development. Thanks to React Hooks and the useContext API. As a result, handling global state management in React apps is made considerably simpler and clearer.

## Redux 
Redux is a predictable state container for JavaScript apps.
It helps you write applications that behave consistently, run in different environments (client, server, and native), and are easy to test. On top of that, it provides a great developer experience, such as live code editing combined with a time traveling debugger.

## Terminologies in Redux implementation 
**Actions**
Actions are objects that are used to send data to the Redux store. They normally have two properties: a payload property that holds the data that needs to be modified in the app state and a type property that describes what the action does.

```javascript
const reduxAction = (payload) => {
  return {
    type: 'ADD_TO_CART',
    payload
  }
};

export default reduxAction;
```
**Reducers**
Reducers are simple functions that carry out the behavior of the action. They begin with the application's current state, take action, and then return a new state.

![Reducer](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/je7ew4wx7xnet0qt07hc.png)
**Store**
The store is where the application's state is kept. In any Redux application, there is just one store:

```js
import { createStore } from 'redux'

const store = createStore(componentName);
```

## React Context API
Context provides a way to pass data through the component tree without having to pass props down manually at every level.
It allows you to share data that can be considered global for a tree of React components, like the current authenticated user, theme, or preferred language.

### Creating a context API

```js
import React, {createContext} from 'react';

const myContext = createContext({ username: 'qbentil' });
```
The above code snippet create a context API using the createContext from reactjs.
The createContext from react return and object with two important properties, `Provider` and `Consumer`.
```js
const { Provider, Consumer } = myContext;
``` 
The Provider component makes the state available to all child components, no matter how deeply nested they are within the component hierarchy whiles the Consumer consumes the data from the Provider without any need for prop drilling.

## React Hooks
Without relying on component classes, React Hooks is the new method for handling state and life cycle in React components. It was added to the library in version 16.8, and its goal is to make the components less complex by transferring logic between them.

> React Hooks provides an easy way of handling the component behavior and share the component logic.

The goal of the React Hooks feature is not to replace prior understanding of React terms like lifecycle, state, props, context, and refs.

## Conclusion
Redux and React Hooks should be viewed as both complementary and distinct concepts. Redux can be used to assist you manage the application data in projects of greater complexity, even though the new React Hooks additions useContext and useReducer allow you to control the global state.

Both can be utilized if you're planning to construct an application. React Hooks features handle the local component state while Redux manages the global state and actions that can be dispatched.

Happy Hacking!

Bentil here🚀
Which of the state management tools mentioned is your favorite? Share your experience in using it below. It might be helpful to someone else too. I will be glad to hear from you in the comment section.

If you find this content helpful, Please Like, comment and share