02Geek HTML5 and JavaScript, TypeScript, React, Flash, ActionScript online School
Previous VideoNext Video

Creating our First Reducer

Creating Our First Reducer: Introduction to Redux Basics

In this lecture, we take our first steps into Redux by creating a simple reducer. This initial implementation won't be fully backed yet, but it will provide a solid foundation for understanding the basic concepts behind reducers and how they fit into the Redux architecture. Reducers are a core part of how Redux operates, helping us manage and transform state in a predictable way.

What is a Reducer?

In Redux, a reducer is a function that takes the current state and an action as arguments and returns a new state. The concept is derived from the idea of reducing multiple inputs into a single output. Reducers are pure functions—they don't modify the existing state directly; instead, they return a new version of the state based on the given action. This makes it easier to manage and predict state changes, which is a key principle of Redux.

Reducers are inspired by Flux, a unidirectional data flow pattern, and were designed by Dan Abramov as a way to simplify state management and make data changes more predictable and traceable.

Setting Up the Redux Store

To start building our first reducer, we need to set up a Redux environment. In this lecture, we create a file named store.js, where we will define our Redux store and the first reducer for our application—a simple counter. This counter reducer will serve as an introductory example to help us understand the basic concepts.

Here's how we define the counter reducer:

function counter(state = 0, action) {
  switch (action.type) {
    case 'STEP_UP':
      return state + 1;
    case 'STEP_DOWN':
      return state - 1;
    default:
      return state;
  }
}

Breaking Down the Key Elements

  1. Reducer Function: The counter function takes in the current state and an action. The state is initialized to 0 if it isn't provided.
  2. Action Types: We use a switch statement to determine the type of action and update the state accordingly. If the action type is 'STEP_UP', the state is incremented by 1. If the action type is 'STEP_DOWN', the state is decremented by 1.
  3. Default Case: If the action type does not match any of the specified cases, the current state is returned without any changes. This ensures the reducer always returns a valid state.

What are Actions?

In Redux, actions are plain JavaScript objects that describe what happened in the application. They must have a type property, which is a string that tells the reducer how to change the state. The type property is often defined as a constant to avoid typos and make the code more maintainable.

For our counter example, we define two action types:

const STEP_UP = 'STEP_UP';
const STEP_DOWN = 'STEP_DOWN';

These constants are used to dispatch actions to the reducer, ensuring that our application state updates correctly.

Connecting the Reducer to the Store

To manage the state with Redux, we need to create a store using the createStore() function from Redux. The store is a centralized state container that allows us to access the current state, dispatch actions, and subscribe to changes.

Here is how we create the store and use the counter reducer:

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

const store = createStore(counter);

Testing the Reducer

Once the store is created, we can test our reducer by dispatching actions and observing the changes in state:

store.dispatch({ type: 'STEP_UP' });
console.log(store.getState()); // 1

store.dispatch({ type: 'STEP_DOWN' });
console.log(store.getState()); // 0

Every time we dispatch an action, the store calls the reducer with the current state and the action, and then the reducer returns a new state based on the action type.

Understanding Pure Functions and State Immutability

One of the reasons Redux is powerful is that it enforces state immutability. Reducers are pure functions; they don't modify the existing state object but instead return a new state. This ensures that the state changes are predictable and easy to debug. Pure functions also make it easier to implement features like time-travel debugging (which allows you to move back and forth between state changes).

Why Use Reducers?

  • Predictable State Updates: By always returning a new state instead of modifying the existing one, reducers make state changes predictable and easy to trace.
  • Pure Functions: Since reducers are pure functions, they provide better reliability and ease of testing.
  • Centralized State Management: When managing complex applications, having a central point for state changes helps in keeping the state consistent across the entire application.

Conclusion

In this lecture, we explored the foundational concept of reducers in Redux and created our first simple reducer. This is an important milestone, as reducers are at the core of how Redux manages state. By understanding how to define a reducer and how actions interact with the state, you have taken the first step towards mastering more complex state management with Redux.

In the next lecture, we will build on this foundation by connecting our reducer to our React components and expanding the capabilities of our Redux setup.

Happy coding!

Ready to Level Up Your Skills?

Join thousands of learners on 02GEEK and start your journey to becoming a coding expert today!

Enroll Now for Free!

Configuring our Project with Create-React-App

Learn how to use create-react-app to quickly set up a React project. This video explains the benefits and step-by-step process of using create-react-app.

06:57

Creating a React Component

Quick recap on creating a React component using JSX and ES6 for beginners.

09:05

Working with the React state

Learn the basics of React state, its limitations, and how to use it effectively before diving into Redux.

08:23

Creating our First Reducer

Learn how to create your first reducer in Redux and understand the foundational concepts behind state management.

05:57

Testing with JEST

Learn how to write automated tests using JEST for Redux, ensuring your application remains stable and reliable.

06:45

Integrating Redux

Learn how to integrate the Redux library by creating a store, utilizing the getState method, and dispatching actions to manage state effectively.

08:00