Redux Vs Context-API

Tanya sharma
4 min readJun 5, 2023

--

As React has risen in popularity as one of the leading JavaScript libraries for front-end development, developers now have multiple options for managing complex application states. Among these options, React Context API and Redux are widely utilised for state management in React applications. While both aim to solve the same problem, they offer distinct approaches and advantages. Consequently, selecting between React Context API and Redux can present a challenging decision for developers, particularly those who are new to React.

What is context API?

React Context API is a part of the React library that allows you to pass data down the component tree without explicitly passing the props through every level. It provides a way to share data between components, even if they are not directly related or nested in each other.

To get started with Context API, there is no additional package or library required, it is a built-in feature in React. You can just simply follow these simple steps to set it up.

  1. Create a Context in a separate file
import { createContext } from 'react';

export const MyContext = createContext("");

2. Create a Provider for the Context that you have created in step 1.

// Create a parent component that wraps child components with a Provider

import { useState, React } from "react";
import { MyContext } from "./MyContext";
import MyComponent from "./MyComponent";

function App() {
const [text, setText] = useState("");

return (
<div>
<MyContext.Provider value={{ text, setText }}>
<MyComponent />
</MyContext.Provider>
</div>
);
}

export default App;

3. Use the Context in your React component

import { useContext } from 'react';
import { MyContext } from './MyContext';

function MyComponent() {
const { text, setText } = useContext(MyContext);

return (
<div>
<h1>{text}</h1>
<button onClick={() => setText('Hello, world!')}>
Click me
</button>
</div>
);
}

export default MyComponent;

Advantages of Context Api :

  1. It simplifies the data-sharing process
  2. It offers a convenient alternative to prop drilling
  3. It allows for efficient updates

Redux

Redux is a popular JavaScript library commonly used with React for managing state in applications. It follows a predictable state container pattern and provides a centralised store to hold the application state.

  1. Install Redux Library
npm install @reduxjs/toolkit react-redux

2. Create a Slice

import { createSlice, PayloadAction } from '@reduxjs/toolkit';

interface State {
count: number;
}

const initialState: State = {
count: 0
};

const counterSlice = createSlice({
name: 'counter',
initialState,
reducers: {
increment: (state) => {
state.count += 1;
},
decrement: (state) => {
state.count -= 1;
}
}
});

export const { increment, decrement } = counterSlice.actions;

export default counterSlice.reducer;

3. Create a redux Store with the Slice that you have created above.

import { configureStore } from '@reduxjs/toolkit';
import counterReducer from './counterSlice';

const store = configureStore({
reducer: {
counter: counterReducer
}
});

export default store;

4. Create the Store that you have created above to your React components

import React, { useCallback } from 'react';
import { useDispatch, useSelector } from 'react-redux';
import { increment, decrement } from './counterSlice';
import store from './store';

const Counter = () => {
const count = useSelector((state: any) => state.counter.count);
const dispatch = useDispatch();

const handleIncrement = useCallback(() => {
dispatch(increment());
}, [dispatch]);

const handleDecrement = useCallback(() => {
dispatch(decrement());
}, [dispatch]);

return (
<div>
<p>Count: {count}</p>
<button onClick={handleIncrement}>Increment</button>
<button onClick={handleDecrement}>Decrement</button>
</div>
);
}

export default Counter;

5. Lastly, wrap your component with a Provider from Redux library and point the store argument to the store that you have created above.

import React from 'react';
import { Provider } from 'react-redux';
import store from './store';
import Counter from './Counter';

const App = () => {
return (
<Provider store={store}>
<Counter />
</Provider>
);
}

export default App;

Here are some advantages of using Redux:

  1. Centralized state management: Redux provides a centralised location for storing the state of an application, which makes it easier to manage and debug.
  2. Predictable state updates: Since the state of an application can only be updated through actions and reducers, the state changes become predictable and easy to reason about.
  3. Time-travel debugging: Redux allows you to trace the state of an application at any point in time, which makes debugging easier.

Differences between React Context API and Redux

When to use React Context API vs Redux?

The decision to use React Context API or Redux depends on the specific needs and complexity of your application.

Use React Context API:
1. Small to medium-sized applications

2. Fewer levels of data sharing

3. Component-specific state:

Use Redux:
1. Large and complex applications

2. Deeply nested components

3. Asynchronous data flow

Ultimately, it’s important to assess the specific needs, complexity, and scale of your application to determine whether React Context API or Redux is the most suitable choice for state management. In some cases, a combination of both tools may be appropriate, with React Context API used for local state management and Redux utilized for global state management.

Conclusion

In summary, React Context API is a simpler solution suitable for smaller applications with simple state management needs, while Redux is a more powerful solution suitable for larger and more complex applications with advanced state management needs.

Ultimately, the choice between React Context API and Redux depends on the unique needs and constraints of your application.

Feel free to review and help me in enhancing the documentation.

Thank you for the reading this blog :)

--

--