Skip to main content

how to use redux with react

· 3 min read
Parth Maheta

React is an incredible library for building user interfaces, but as applications grow in complexity, managing state can become challenging. That's where Redux comes in.

Redux is a state management library that works seamlessly with React to help you manage the state of your application in a more organized and scalable way. In this beginner-friendly guide, we'll explore the basics of Redux with a simple example.

What is Redux?

Redux is a predictable state container for JavaScript applications. It helps you manage the state of your application in a central store, making it easier to track changes, debug, and reason about the state of your app. Redux follows a unidirectional data flow, making it predictable and easier to understand.

Core Concepts of Redux

Before diving into the code, let's understand the core concepts of Redux:

  1. Store: The store is a single JavaScript object that represents the state of your application. It is the single source of truth.

  2. Actions: Actions are plain JavaScript objects that describe changes in the application state. They are the only way to modify the state.

  3. Reducers: Reducers are pure functions that specify how the application's state changes in response to an action. They take the previous state and an action, and return the next state.

  4. Dispatch: The dispatch function is used to dispatch actions to the Redux store. It triggers the state change.

  5. Connect: The connect function is used in React components to connect them to the Redux store. It allows components to subscribe to the Redux store and receive updates when the state changes.

Setting Up Redux in a React App

Let's create a simple React app and integrate Redux into it. First, install the necessary packages:

npm install react-redux redux

Now, let's create a basic Redux setup:

1. Creating Actions

// actions.js
export const increment = () => {
return {
type: 'INCREMENT'
};
};

export const decrement = () => {
return {
type: 'DECREMENT'
};
};

2. Creating Reducers

// reducers.js
const counterReducer = (state = 0, action) => {
switch (action.type) {
case 'INCREMENT':
return state + 1;
case 'DECREMENT':
return state - 1;
default:
return state;
}
};

export default counterReducer;

3. Creating Store

// store.js
import { createStore } from 'redux';
import counterReducer from './reducers';

const store = createStore(counterReducer);

export default store;

4. Connecting React Components

// App.js
import React from 'react';
import { Provider, connect } from 'react-redux';
import store from './store';
import { increment, decrement } from './actions';

const App = ({ count, increment, decrement }) => {
return (
<div>
<h1>Redux Counter</h1>
<p>Count: {count}</p>
<button onClick={increment}>Increment</button>
<button onClick={decrement}>Decrement</button>
</div>
);
};

const mapStateToProps = state => {
return {
count: state
};
};

const mapDispatchToProps = dispatch => {
return {
increment: () => dispatch(increment()),
decrement: () => dispatch(decrement())
};
};

const ConnectedApp = connect(mapStateToProps, mapDispatchToProps)(App);

const ReduxApp = () => {
return (
<Provider store={store}>
<ConnectedApp />
</Provider>
);
};

export default ReduxApp;

Conclusion

Redux can seem overwhelming at first, but as your React applications grow, you'll appreciate its benefits in managing state more efficiently. This guide provides a basic introduction to Redux, and there's much more to explore. Start small, experiment with simple examples, and gradually incorporate Redux into your projects. As you become more familiar with its concepts, you'll find Redux to be a valuable tool for state management in your React applications.