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

Redux Actions with Properties: Creating Bound and Unbound Action Methods

Redux Actions with Properties: Creating Bound and Unbound Action Methods

In this tutorial, we continue our exploration of Redux actions, focusing on creating bound and unbound action methods. We will also discuss how to work with properties that are passed through these actions, enhancing the flexibility of your state management.

Extracting Actions

In the last session, we discussed extracting actions into separate files to make them more modular and reusable. We created bound and unbound action methods, which can either automatically dispatch or be dispatched explicitly. This session builds on that concept, extending it to handle actions with properties.

Step Up and Step Down Actions with Properties

Consider the scenario where we have step up and step down actions in a counter application. These actions need to pass specific properties, such as an index, to update the correct state. Let's see how we can implement these actions.

Step 1: Create Action Methods

First, let's define the stepUp and stepDown actions. We start by exporting the methods in our actions.js file:

// actions.js
import { STEP_UP, STEP_DOWN } from './actionTypes';

// Step Up Action
export const stepUpAction = (index) => {
    return {
        type: STEP_UP,
        index
    };
};

// Step Down Action
export const stepDownAction = (index) => {
    return {
        type: STEP_DOWN,
        index
    };
};

In these methods, the index is a parameter that allows us to specify which counter we are updating. The action type (STEP_UP or STEP_DOWN) determines whether we are incrementing or decrementing the value.

Step 2: Create Bound Actions

Next, we define bound actions, which are automatically dispatched when called. These actions will interact with the Redux store.

// actions.js
import { store } from './store';

// Bound Step Up Action
export const boundStepUp = (index) => {
    return store.dispatch(stepUpAction(index));
};

// Bound Step Down Action
export const boundStepDown = (index) => {
    return store.dispatch(stepDownAction(index));
};

These bound actions use store.dispatch() to directly dispatch the action. When you call boundStepUp(index), it will immediately trigger the dispatch, updating the state accordingly.

Using Bound and Unbound Actions in Components

Now that we have both bound and unbound actions, we can integrate them into our components. Let's look at how to use these actions in a counter component.

Example: Counter Component

import React from 'react';
import { boundStepUp, boundStepDown } from './store/actions';

const CounterComponent = ({ index, count }) => {
    return (
        <div>
            <p>Counter: {count}</p>
            <button onClick={() => boundStepUp(index)}>Step Up</button>
            <button onClick={() => boundStepDown(index)}>Step Down</button>
        </div>
    );
};

export default CounterComponent;

In the CounterComponent, we use boundStepUp and boundStepDown to modify the counter's value. These bound actions make it easy to update the state without having to manually call dispatch() in the component.

Why Separate Bound and Unbound Actions?

Separating bound and unbound actions has several advantages:

  1. Flexibility: Unbound actions can be used in more complex workflows where additional logic is needed before dispatching.
  2. Code Reusability: By keeping actions separate from dispatch, we can reuse them in different contexts without duplicating code.
  3. Readability: It’s easier to read and understand code when actions are organized clearly into separate, single-purpose functions.

Best Practices for Action Methods with Properties

When creating actions that take properties, consider the following best practices:

  1. Keep Action Methods Pure: Action methods should simply return the action object. Avoid adding complex logic here; keep it pure for predictability.
  2. Use Bound Actions for Convenience: Use bound actions for convenience in components where the dispatch step is straightforward and doesn’t need additional logic.
  3. Avoid Tight Coupling: Keep your components decoupled from Redux as much as possible by using well-defined actions and action creators.

Summary

  • Unbound Actions: Functions that return action objects, allowing for additional logic before dispatching.
  • Bound Actions: Functions that dispatch actions automatically, providing a simpler interface for use in components.
  • Properties in Actions: When actions need to carry properties (e.g., index), define those parameters clearly in the action creators.
  • Best Practices: Keep actions pure, use bound actions for simple dispatches, and avoid tight coupling between Redux and UI components.

By following these practices, you'll create maintainable, scalable Redux actions that are easy to work with and reuse across your application.

What's Next?

In the next tutorial, we'll explore how to leverage Provider from React-Redux to make the store accessible throughout your application and see how we can structure the store to improve data flow management.

Video Transcript

00:01In the last lecture, we extracted out our actions
00:04to be in their own unique functions that could dispatch
00:08or not, we called them bound or unbound actions.
00:11And we're gonna continue down this path,
00:13but this time around our action methods
00:14and bound action methods will deal with properties
00:18that they need to pass through their actions.
00:21Alright, so we wanna do the same thing as we've done
00:24for our app action, which was to add a counter.
00:27We wanna do the same thing for our counter itself.
00:30'cause in our counter itself we have your, uh, our step up
00:33and our step down that our both using these bound methods,
00:37although in our case we, it's a little bit different here
00:40because now we also have a property
00:41that we want to send out.
00:43So let's go ahead and try to implement these methods
00:45and I'm just gonna go ahead and copy them
00:47and place them inside of our actions
00:49and start modifying them.
00:50So first of all, let's go ahead.
00:51We're gonna export our step up
00:55and our step up is going basically retrieve one variable,
00:58which will be our index.
01:00And once it gets that index, it's gonna go ahead
01:03and return our step up index
01:09And it's gonna take it from our types.
01:12And we also want to create a bound step up.
01:15So this, I'm gonna call this one step up action
01:19and I'm gonna go ahead here and create another step up.
01:22Only this step up is going
01:24to be an actual step up without an action.
01:27And in this case, we also wanna send in our index only this
01:31time around when we're actually gonna call
01:33our store dispatch.
01:34What we're gonna do is we're also gonna send into that call
01:38set, step up action, the index that we've just retrieved.
01:46Okay, so there we go.
01:47So the steps involved so far is we have your step up
01:51and then we've internally very similarly
01:54to the way we've done it in the previous example,
01:56only this time around, we're also sending a property.
01:59Now let's go ahead and do the same thing,
02:01but this time around we're gonna do it
02:02also for our step down.
02:04So, uh, that was a step up, step down,
02:09step down, step down.
02:15And we want to actually step down
02:19and we could delete our step down method.
02:21Okay? So we've created now another four methods.
02:24Two of them are for our step up.
02:26One of them is an action, one
02:27of them is an actually abounded action.
02:30And let's go ahead and implement the bounded action.
02:33Now, the reason why we have this separation also
02:35'cause there could be cases where we do not want
02:37to actually trigger our dispatch meant as soon
02:40as we start our mission, for example, but in our case we do.
02:44So I'm gonna go ahead here
02:45and what I want to do is I'm gonna load up
02:47the correct items.
02:48So I wanna load up, step up and step down.
02:54I wanna load them up from our actions.
02:57And then what we're gonna do is I still need this index.
03:00So I'm just gonna go ahead here
03:01and call the step up this time
03:03around our external method here.
03:06And we're just gonna literally just send into it our index.
03:11And I'm gonna do the exact same
03:12thing only for stepping down.
03:13And we're gonna do that for our step down
03:19and making sure that we don't call the dispatch anymore.
03:22And now that we've done that,
03:23we could literally extract out our store.
03:25We don't need to actually approach our store directly from
03:28within our counter as well.
03:30So what we've done in this session
03:32and this lecture so far in the two lectures combined
03:34as we extracted out all the different places
03:37where we were actually calling actions directly.
03:40So literally we got rid of every place where we had to call,
03:44uh, directly.
03:45The dispatcher call directly an action.
03:47And instead of that we're gonna be calling all these
03:49mediators that make it easier for us.
03:52So to visually see all the different actions
03:54that are happening in, in our application.
03:56So in our case, it might make sense
03:58to put this into an actions folder
04:00and call this the counters
04:02because it's all related to counting on contrary
04:06to if we have more actions that relate to boards
04:08that currently we did not implement any of them,
04:10then it would make sense to have you're an actions folder.
04:12And instead of our actions folder,
04:14we would have all the different actions related specifically
04:17for our um, counter.
04:20But in our case, because our application is more simple than
04:23that, then we don't really have to go through that effort.
04:26At this stage in this lecture, we saw how
04:30to integrate actions that have properties,
04:33and in the next lecture we're gonna continue down the path
04:36and actually take a split from the structuring
04:38that we were doing so far to take a look at the solution
04:42provided by Dan the creator of Redux and also React Redux.
04:47And we're gonna take a look at how to use a provider
04:49to provide our data to all the internal components.

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!