React Redux | PART 2

Pranav Bakare - Oct 8 - - Dev Community

Certainly! Here’s a detailed explanation of the key components of Redux in a React application, along with a simple example that demonstrates their usage.

Example Redux Setup in a React Application

Let’s create a simple counter application that increments and decrements a count using Redux.

Step 1: Install Redux and React-Redux

First, make sure you have Redux and React-Redux installed in your React application:

npm install redux react-redux

Step 2: Create the Redux Store

Create a store.js file to set up the Redux store.

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

// Initial state
const initialState = {
count: 0,
};

// Action Types
const INCREMENT = 'INCREMENT';
const DECREMENT = 'DECREMENT';

// Action Creators
export const increment = () => ({ type: INCREMENT });
export const decrement = () => ({ type: DECREMENT });

// Reducer
const counterReducer = (state = initialState, action) => {
switch (action.type) {
case INCREMENT:
return { ...state, count: state.count + 1 };
case DECREMENT:
return { ...state, count: state.count - 1 };
default:
return state;
}
};

// Create Store
const store = createStore(counterReducer);

export default store;

Explanation of Components in the Example

  1. Store: The store is created using createStore(counterReducer). This store holds the application's state.

  2. Actions:

We define two action types, INCREMENT and DECREMENT.

Action creators (increment and decrement) return action objects.

  1. Reducer:

The counterReducer function takes the current state and an action. It returns a new state based on the action type. We use the spread operator (...state) to ensure immutability.

Step 3: Set Up the Provider

In your main application file (e.g., index.js), wrap your App component with the Provider to make the Redux store available to all components.

// index.js
import React from 'react';
import ReactDOM from 'react-dom/client';
import { Provider } from 'react-redux';
import App from './App';
import store from './store';

const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(



);

Step 4: Create the Counter Component

Now, let’s create a Counter component that connects to the Redux store to display and update the count.

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

const Counter = () => {
// Access the count state
const count = useSelector((state) => state.count);
const dispatch = useDispatch();

return (


Counter: {count}


dispatch(increment())}>Increment
dispatch(decrement())}>Decrement

);
};

export default Counter;

Explanation of the Counter Component

  1. useSelector: This hook is used to access the Redux store's state. We select state.count to get the current count.

  2. useDispatch: This hook returns the dispatch function. We use it to dispatch actions when the buttons are clicked.

  3. Rendering: The component displays the current count and buttons to increment or decrement the count.

Step 5: Integrate the Counter Component

Finally, integrate the Counter component into your main App.js file.

// App.js
import React from 'react';
import Counter from './Counter';

const App = () => {
return (


My Redux Counter App




);
};

export default App;

Conclusion

Now you have a complete Redux setup in a React application. The key components are:

Store: Holds the entire state.

Actions: Define the changes to the state.

Action Creators: Functions that create actions.

Reducer: Manages how the state changes in response to actions.

Provider: Makes the store available to your components.

Connect: Use hooks like useSelector and useDispatch to interact with the store.

Running the Application

To run your application, use the following command:

npm start

This will launch your app, and you should see the counter functionality working as expected. You can click the "Increment" and "Decrement" buttons to update the count, which will reflect in the UI due to the Redux state management.

This example illustrates how to effectively structure a simple Redux application within a React context, highlighting each component's role in managing application state.

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Terabox Video Player