How to use React Redux for state management

0 votes

How to use React Redux for state management?

I'm trying to use React Redux for state management in my application. Can someone help me understand how to set it up and use it effectively?

Dec 13, 2024 in Web Development by Nidhi
• 5,440 points
37 views

1 answer to this question.

0 votes

To use React Redux for state management, follow these steps:

Install Redux and React-Redux:

npm install redux react-redux

Create Redux actions: Actions are plain objects that describe an event.

// actions.js

export const increment = () => ({ type: 'INCREMENT' });

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

Create a reducer: A reducer specifies how the state should change based on an action.

// reducer.js

const initialState = { count: 0 };

const counterReducer = (state = initialState, action) => {

  switch (action.type) {

    case 'INCREMENT':

      return { count: state.count + 1 };

    case 'DECREMENT':

      return { count: state.count - 1 };

    default:

      return state;

  }

};

export default counterReducer;

Create the store: The store holds the entire state of your app.

// store.js

import { createStore } from 'redux';

import counterReducer from './reducer';

const store = createStore(counterReducer);

export default store;

Wrap your app with Provider: This makes the Redux store accessible to all components.

// index.js

import React from 'react';

import ReactDOM from 'react-dom';

import { Provider } from 'react-redux';

import store from './store';

import App from './App';

ReactDOM.render(

  <Provider store={store}>

    <App />

  </Provider>,

  document.getElementById('root')

);

Connect components using useSelector and useDispatch:

// App.js

import React from 'react';

import { useSelector, useDispatch } from 'react-redux';

import { increment, decrement } from './actions';


const App = () => {

  const count = useSelector(state => state.count);

  const dispatch = useDispatch();

  return (

    <div>

      <h1>Count: {count}</h1>

      <button onClick={() => dispatch(increment())}>Increment</button>

      <button onClick={() => dispatch(decrement())}>Decrement</button>

    </div>

  );

};

export default App;

answered Dec 13, 2024 by Navya

Related Questions In Web Development

0 votes
0 answers
0 votes
1 answer

How to decide when to use replicate sets for mongodb in production?

Here's a breakdown of when to consider ...READ MORE

answered Nov 13, 2024 in Web Development by kavya
100 views
0 votes
0 answers

How to start Airbnb Clone for Movies and Events Booking Website?

Instant #Online Movies and Events Ticket Booking ...READ MORE

Feb 17, 2020 in Web Development by Bessie
• 160 points

reshown Feb 18, 2020 by Gitika 881 views
0 votes
1 answer

How to set up authentication for reactjs and django ?

Hello  yahya , Using both, you can secure ...READ MORE

answered Jun 2, 2020 in Web Development by Niroj
• 82,840 points
1,910 views
0 votes
1 answer

How can I remove a port from url for node app using nginx

If you run your node server on ...READ MORE

answered Apr 10, 2018 in DevOps on Cloud by ajs3033
• 7,300 points
4,131 views
0 votes
4 answers

ReactJS vs Angular Comparison: Which is better?

Parameters React Angular Type React is a JavaScript library, and it ...READ MORE

answered Jan 7, 2021 in Events & Trending Topics by Focusteck
• 140 points
1,880 views
+2 votes
4 answers
0 votes
1 answer

How to handle manage global state across deeply nested components without Redux?

Firstly , we should know what is ...READ MORE

answered Oct 21, 2024 in Web Development by Navya
• 380 points
131 views
0 votes
1 answer

How to use async functions in Saga Middleware with React?

The call effect, which is used to ...READ MORE

answered Dec 31, 2024 in Web Development by Navya
40 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP