This lesson is for PRO members.

Unlock this lesson NOW!
Already subscribed? sign in

Connecting to a Reflux Store in React

3:37 React lesson by

Reflux makes connecting to a store extremely simple. Let's refactor a React component so that the data now comes from a Reflux store and allows the data store to trigger future updates.

Get the Code Now
click to level up

egghead.io comment guidelines

Avatar
egghead.io

Reflux makes connecting to a store extremely simple. Let's refactor a React component so that the data now comes from a Reflux store and allows the data store to trigger future updates.

Avatar
Gustavo Saiani

Superagent was at version 1.1.0 when I installed it.

For the request to work, I replaced

init() {
    request("https://jsbin.com/dekafa.json", res => {
      this.data.people = JSON.parse(res.text);
      this.trigger(this.data);
    })
  },

with

init() {
    var that = this

    request
      .get("https://jsbin.com/dekafa.json")
      .end(function(err, res) {
        that.data.people = JSON.parse(res.text)
        that.trigger(that.data)
      })
  },
In reply to egghead.io
Avatar
Nicolas

I'm so impressed with how this guy teaches!

To connect this simple demo up to a Reflux store, I'm first going to move this data we're displaying into a state, so this.state.message.

Then we'll say, "getInitialState" and return message of "Hello from state." Again, this is the shorthand ES6 syntax for functions on an object. Once I refresh here, you'll see it says, "Hello from state."

Now, I can go ahead and install Reflux. Once I have Reflux installed, I can say, "require("reflux")," and then, Reflux is what I required. Then, use Reflux to create a store, which is just going to be an object where I'm going to cut getInitialState out of the component and paste it into the store.

Then, to hook these up together, I'll say, "var store." That's something that I can pass into mixins, which is just basically, functionality, you can mix into your components, which takes an array.

I'll say, "Reflux.connect," and I'll say, "store." Once I save, hit refresh, it'll still work. Just to make sure it's coming from the store, I'll say, "Hello from store." Refresh again. We have "Hello from store."

What's going on here is this Reflux.connect helper is grabbing a store. Any time it triggers an update, it's going to say, "Set state," or when it requests the getInitialState, it's going to get it from this.

To show you how you can trigger an update, I'll go ahead and set up an init. Init with a setInterval, which takes a function. I'm using the arrow function, because I want this scoped to my store.

I'll say, "this.trigger." Any data I pass into this.trigger, for example, an object with message on it, that says, "Something different."

Then, we'll call this every second. I'll go ahead and hit "save." Refresh. You can see after one second, it changes to something different. To see this more in action, I'm actually going to move this out of here.

Instead of returning an object, I'll say, "return this.data." I'll go ahead and set up a data on here, which will just start as an object with a message of "0."

Then, in my setInterval, I can say, "this.data.message++." Every time I trigger it, I can say, "this.data" and just send that along. Save. Refresh. You'll see "0, 1, 2, 3" and so on. You'll notice that I didn't change anything else in the component itself. It's really connected to this store that I created.

It's calling that set state, which is updating this.state.message every time that this.trigger gets called on the store. We get that initial state, which is just the data.message.

Then, every second, we're updating the message. Then, we're triggering it so that we send that new data along, which, again, gets called as set state on our component.

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