Notes
JavaScript Front-End Development
What is React Redux

Introduction to React Redux

What is Redux?

Redux is a predictable state container for JavaScript apps. It helps you manage the state of your application by centralizing it in a single source of truth, known as the store. This makes it easier to manage state in larger applications, where components may need to share and update data frequently. Redux is a popular state management library often used in React applications to manage complex state across components. It provides a predictable way to manage state by following a unidirectional data flow, making it easier to understand, debug, and maintain your application.

Key Concepts in Redux

  • Store: The store holds the entire state of your application. There is only one store in a Redux application.

  • Actions: Actions are plain JavaScript objects that describe a change you want to make to the state. Each action has a type property and may include additional data.

    const incrementAction = {
      type: 'INCREMENT',
    };
  • Reducers: Reducers are pure functions that take the current state and an action as arguments and return a new state. The reducer specifies how the state changes in response to actions.

    const counterReducer = (state = 0, action) => {
      switch (action.type) {
        case 'INCREMENT':
          return state + 1;
        case 'DECREMENT':
          return state - 1;
        default:
          return state;
      }
    };
  • Dispatch: The dispatch function is used to send actions to the store. The store then uses the reducer to update the state based on the action dispatched.

    store.dispatch(incrementAction);

Why Use Redux with React?

React by itself can manage component-level state using useState and useReducer. However, as your application grows, managing state across multiple components can become challenging. Redux helps by providing a global state that all components can access and modify in a predictable way.

Benefits of Using Redux:

  • Predictable State Management: Redux follows strict rules for how the state is updated, making it easier to understand and debug your application.

  • Centralized State: All your application state is stored in one place, making it easier to manage and maintain.

  • Time Travel Debugging: Redux DevTools allow you to inspect every state change in your application and even "time travel" back to a previous state.

Basic Redux Setup in React

Here's a simple example of setting up Redux in a React application.

1. Install Redux and React-Redux

First, you'll need to install Redux and React-Redux, which is the official React binding for Redux.

npm install redux react-redux

2. Create a Redux Store

Create a store.js file and set up your Redux store.

import { createStore } from 'redux';
 
// Define your reducer
const counterReducer = (state = 0, action) => {
  switch (action.type) {
    case 'INCREMENT':
      return state + 1;
    case 'DECREMENT':
      return state - 1;
    default:
      return state;
  }
};
 
// Create the store with the reducer
const store = createStore(counterReducer);
 
export default store;

3. Provide the Store to Your React Application

Wrap your main application component with the Provider component from React-Redux, passing the store as a prop.

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')
);

4. Connecting Components to the Store

Use the useSelector and useDispatch hooks from React-Redux to interact with the store in your components.

import React from 'react';
import { useSelector, useDispatch } from 'react-redux';
 
const Counter = () => {
  const count = useSelector((state) => state);
  const dispatch = useDispatch();
 
  return (
    <div>
      <p>Count: {count}</p>
      <button onClick={() => dispatch({ type: 'INCREMENT' })}>Increment</button>
      <button onClick={() => dispatch({ type: 'DECREMENT' })}>Decrement</button>
    </div>
  );
};
 
export default Counter;

Additional Resources

By using Redux with React, you can manage the state of your application more effectively, especially as it grows in complexity.