This lesson is for PRO members.

Unlock this lesson NOW!
Already subscribed? sign in

React Flux: Actions

4:37 React lesson by

Actions contain no functionality, but rather describe an event in our application. In this lesson we will describe our Actions as they relate to our application. These Actions will then be available for triggering in our Views and for execution in our Store.

Get the Code Now
click to level up

egghead.io comment guidelines

Avatar
egghead.io

Actions contain no functionality, but rather describe an event in our application. In this lesson we will describe our Actions as they relate to our application. These Actions will then be available for triggering in our Views and for execution in our Store.

Avatar
Sean Coonce

What's the best way to scale app-actions.js as your app becomes more and more complex? Is it suitable to simply append all of your apps actions to what could become a potentially massive file?

Avatar
Joel

What's the best way to scale app-actions.js as your app becomes more and more complex? Is it suitable to simply append all of your apps actions to what could become a potentially massive file?

I'm not an expert, but as I see it, you are dividing your application up into many small applications, so each would have actions that corresponded to the functionality of the individual module.

Perhaps Flux gives a better idea about this in practice.

In reply to Sean Coonce
Avatar
Gabriel

It's important to remember, that this Lesson use the 'react/lib/merge', and this lib 'll be deprecated on the next version of React, if you want to keep your code up to date, run the following line in your terminal (make sure to execute this inside your project directory):
npm install object-assign --save

After that, replace this:
var merge = require('react/lib/merge');

for this:
var assign = require('object-assign');

And, everywhere that you use the 'merge', you replace with 'assign';

Avatar
Omer

I used the dispatcher from Facebook’s Chat Example to build the dispatcher and it should look like this now:

var Dispatcher = require('flux').Dispatcher;
var assign = require('object-assign');
var AppDispatcher = assign(new Dispatcher, {
handleViewAction: function(action){
console.log('action', action);
this.dispatch({
source: 'VIEW_ACTION',
action:action
})
}
});

module.exports = AppDispatcher;

Avatar
Christopher

be sure to:
npm install flux --save

if you use this dispatcher

In reply to Omer
Avatar
Christopher

Thanks for these corrections! They work!

In reply to Omer
Avatar
Sean Inglis

Thanks for this Gabriel - spent a bit of time blundering around in the source tree

I've been manually following the videos as a way of bumping up my React muscle memory so haven't benefitted from any corrections in the downloadable source.

In reply to Gabriel
Avatar
Lachlan

Thanks mate appreciate it.

In reply to Gabriel
Avatar
Cyrus

Thanks for posting this man! It really helped!

In reply to Omer
Avatar
Alexius Wronka

I need to create and add users to a MongoDB with react. Will this tutorial help?

The first step in creating our app actions is actually going to be to create a list of those in our app constants. I'm creating app-constants.js in the constants directory. Here, we're just going to export a very simple object that's going to list each of those actions by name. Sorry, ADD_ITEM.

We're going to be building a shopping cart. We're going to have some actions that relate to adding and removing items from a shopping cart. This one's going to be REMOVE. Then, we'll have INCREASE_ITEM.

Finally, we'll have DECREASE_ITEM. Cool. We've got our constants. Now we can go ahead and...Whoops. A little typo there.

Now, we can go ahead and create our actual actions file. Here in the actions directory, I'm going to create app-actions.js. Here, we're going to need our AppConstants, and we're also going to need our AppDispatcher.

We can go ahead and begin building up our AppActions object, which is what we'll export when we're done here. We're just going to go ahead and create those. The first one's going to be addItem. It's going to be a function. Takes in an item. What it's going to do is it's going to call AppDispatcher.handleViewAction. It's going to take in an object.

If we go back and look at our AppDispatcher, that is the key that we added. We've got this handleViewAction method available to us. Into that, we are going to pass an actionType, which is going to come from our AppConstants.ADD_ITEM, which we just created, obviously. We're also going to pass the actual item in itself. This is adding an item to our shopping cart.

We're going to copy this guy here. This is going to be our removeItem. Since we don't really need the item...we need the index of the item in our cart...we're going to be passing in index. Our actionType here is going to be REMOVE_ITEM. Here, rather than passing in the item, we're going to be passing in the index.

We can actually copy this guy two more times. This will be increaseItem. increaseItem there. That function will work the exact same way. This will be decreaseItem. All that's looking good. We'll go ahead and export our AppActions. Save that guy out.

Our next step is actually to create our stores. However, if we want to get a look at how this is going to work within our application...I'm going to go ahead and add AppActions here, to our App component. I'm going to create a method here, called "handler." It's going to be a function.

Here, we're just going to call AppActions.addItem, which is a method we just created. Rather than passing in an actual item, I'm going to say, "this is an item," as a string. We'll trigger that on the onClick event of our h1 here, so this.handler. Save that.

If we jump over here, to our Dispatcher, right here, in our handleViewAction function, we'll just output or we'll console.log out "action" and the action that's passed in. Go ahead and save that guy.

Reload over here and bring up our console. Here's our console. When we click on this guy...whoops. Typo there. AppActions. Sorry about that.

Reload over here. We click on him. We can see our actual action is being sent to the Dispatcher. We're not doing anything with it yet, because we haven't created our store, but that'll be the next step.

HEY, QUICK QUESTION!
Joel's Head
Why are we asking?