This lesson is for PRO members.

Unlock this lesson NOW!
Already subscribed? sign in

Reflux - Manually Triggering Actions

4:18 React lesson by

Reflux actions don't need to be used within React components, you can trigger them from anywhere. John demonstrates this concept by extracting the actions away and then invoking them through jquery.

Get the Code Now
click to level up

egghead.io comment guidelines

Avatar
egghead.io

Reflux actions don't need to be used within React components, you can trigger them from anywhere. John demonstrates this concept by extracting the actions away and then invoking them through jquery.

Avatar
Tristan

I find this interesting, as I've been attempting to convert over the flux app example over to reflux and this was one of the really interesting parts about it I found.

here's my working version https://github.com/darcnite3000/egghead-react--re-flux-example

Avatar
Dean

I'm curious why, in the first part of the video, you are are just setting message via direct assignment rather than using setState ?

In reply to egghead.io

Using Reflux you don't have to use any sort of react events to trigger actions. You can just manually trigger them from anywhere. I'm going to start this example up using an initial state which returns a message of initial state, which renders right here. You can see it shows in the browser.

I'll create our actions. Just var-action is reflux.createActions with an array of strings of one, two, and three. To hook these actions up to the store I will go down and say listenables, which is an array, which takes my actions.

Then I can just create the handles which look like on one and this .trigger which we'll do to this .setstate on my component, so the message is just one triggered. Then I can duplicate this guy and make on two and say two triggered and on three and three triggered.

Now all I have to do is say actions.one and invoke it, and you can see in the browser it will say one triggered. This is because my actions one triggered on one which sent the message of one triggered down into the component this.state.message and rendered it out.

I'll go ahead and delete actions.one. Just to show all these actions are working, I'll do some set timeouts. Set timeout passing the actions.one function. After one second duplicate this, actions.two, two seconds and actions.three, three seconds.

When the browser saves and refreshes after one second, two seconds, three seconds, you can see each of them are triggered. Now that we're calling all of these manually, let's move them into another file. I'll create a new JavaScript file and name it actions.js.

Basically I'm going to copy and paste or cut and paste these actions into my actions file. I'll use module.exports and just export my actions. Because I do have Reflux in here, I will require Reflux as well. Then I'll just go ahead and require my actions, so var-actions=require actions. Then everything will work just like it did before.

Just to demonstrate you can call these from absolutely anywhere, I'll go ahead and create another file, another JavaScript file, and we'll just call it something else. I'll require jQuery this time. Then I'll also require our actions. Then I'll just switch over to our HTML, set up a button with an id of button.

I can trigger actions too, so now we'll have a button that says, "I can trigger actions too." I'll click on it, and nothing happens. But when I switch over to my something else I'll set up a jQuery, look up the id button, set up a click handler which will just take the event and use an arrow function to call actions.one.

Then switch over to my app and require something else. You can see that once these are finished I can now start clicking on the button, and I'll get one triggered. This is because it's calling one. If I switch it to two, now I can trigger the two action. There's no reactant here. It's just jQuery firing an action which is being handled in the store.

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