Getting Started with Redux in React

Tutorial 1 of 5

Getting Started with Redux in React

1. Introduction

This tutorial aims to introduce Redux, a predictable state container for JavaScript applications, and how to use it within a React application. We'll discuss how to set up Redux, create a store, and interact with it using actions and reducers.

By the end of this tutorial, you'll be able to understand the basic concepts of Redux, integrate it into a React application, and be able to manage the state of your app more predictably and efficiently.

Prerequisites:
- Basic knowledge of JavaScript and React.

2. Step-by-Step Guide

Redux is used for managing the state of your application. The state of an application is represented by a JavaScript object, and each modification to the state returns a new state object.

Redux has three fundamental principles:
1. Single source of truth: The state of your whole application is stored in an object tree within a single store.
2. State is read-only: The only way to change the state is to emit an action, an object describing what happened.
3. Changes are made with pure functions: To specify how the state tree is transformed by actions, you use pure reducers.

Setting Up Redux

First, you need to install Redux and React-Redux, a binding layer between Redux and React:

npm install redux react-redux

Creating a Store

A Redux store is where your app's state is actually stored. Here's how you can create a store:

import { createStore } from 'redux';
import rootReducer from './reducers';

const store = createStore(rootReducer);

In the above code, rootReducer is your application's main reducer.

Interacting with the Store

Redux provides a few methods to interact with the store:

  • store.dispatch(action): Dispatches an action to the store.
  • store.subscribe(listener): Adds a change listener. The listener function will be called any time an action is dispatched.
  • store.getState(): Returns the current state of the store.

3. Code Examples

Creating an Action

Actions are plain JavaScript objects that have a type field.

const ADD_TODO = 'ADD_TODO';

function addTodo(text) {
  return {
    type: ADD_TODO,
    text
  };
}

Creating a Reducer

Reducers specify how the state changes in response to actions.

function todoApp(state = initialState, action) {
  switch (action.type) {
    case ADD_TODO:
      return Object.assign({}, state, {
        todos: [
          ...state.todos,
          {
            text: action.text,
            completed: false
          }
        ]
      });
    default:
      return state;
  }
}

4. Summary

We've covered the basics of Redux in a React application, including how to set up Redux, create a Redux store, and interact with the store using actions and reducers.

Next steps for learning include diving deeper into advanced Redux concepts, like middleware and asynchronous actions.

5. Practice Exercises

  1. Create a Redux store, dispatch an action to it, and log the new state.
  2. Write a reducer for handling the above action.
  3. Integrate the Redux store with a simple React component.

Remember, practice is key when learning new concepts. Happy coding!