This lesson is for PRO members.

Unlock this lesson NOW!
Already subscribed? sign in

React Flux: Components/Views

9:39 React lesson by

In Flux our Components are referred to as Views, but they are the same familiar Components associated with any React development. In Flux, however, we have the benefit of the architecture to keep us from having to pass down through a long chain of children Components any functionality that may be embedded in our Stores. In this Lesson we will wire up all of our Components in record time utilizing the architecture we've already established.

Get the Code Now
click to level up

egghead.io comment guidelines

Avatar
egghead.io

In Flux our Components are referred to as Views, but they are the same familiar Components associated with any React development. In Flux, however, we have the benefit of the architecture to keep us from having to pass down through a long chain of children Components any functionality that may be embedded in our Stores. In this Lesson we will wire up all of our Components in record time utilizing the architecture we've already established.

Avatar
Martino

Firtst of all: nice serie :), btw it seems to me that the companion app.js code is not updated with the video. Besides this, is it possible to have the a downloadable version of the code?

In reply to egghead.io
Avatar
Tarjei Huse

What I miss is a discussion of how to handle errors in the flux architecture. Maybe an idea for a new movie?

Avatar
Todd

The app.js file included in this source does not match the video.

In reply to egghead.io
Avatar
Christopher

in app-store.js I had to make the following change:

var AppStore = assign({}, EventEmitter.prototype, {
    emitChange:function(){
        this.emit(CHANGE_EVENT)
    },

to avoid React key warnings in app-catalog.js :

var Catalog =
    React.createClass({
        getInitialState: function(){
            console.log(getCatalog());
            return getCatalog();
        },
        render: function(){
            var items = this.state.items.map(function(item){
                return <tr key={item.id}><td>{item.title}</td><td>${item.cost}</td><td><AddToCart item={item} /></td></tr>
            });
            return (
                <table className="table table-hover">
                    {items}
                </table>
            )
        }
    });
Avatar
Stephen

Yep, App.js example file was not updated.

/** @jsx React.DOM */
var React = require('react');
var Catalog = require('../components/app-catalog.js');
var Cart = require('../components/app-cart');
var APP =
    React.createClass({
        render: function () {
            return (
                <div>
                    <h1>Lets shop</h1>
                    <Catalog />
                    <h1>Cart</h1>
                    <Cart />
                </div>
            )
        }
    });
module.exports = APP;
Avatar
Sean Inglis

I've made the change suggested by others here (thanks!) but still come unstuck.

Tracing as best I can, when I click on the Add To Cart button, I can see the handleClick function fire, the VIEWACTION being dispatched from the app-dispatcher with an ADDITEM actionType, addPromise being called in the dispatch method of the base dispatcher, but Promise.all(promises).then(_clearPromises); never seems to resolve and fire the "then" clause.

Not of the events seem to be emitted.

Avatar
Vincent

Would be nice if the files provided worked/up to date :(
even after following advice above still getting:

Warning: Each child in an array or iterator should have a unique "key" prop. Check the render method of Catalog. See http://fb.me/react-warning-keys for more information.

Avatar
Fareez

I'm getting the following error: "module "react/lib/merge" not found"

I'm guessing this stems from the boilerplate dispatcher code. Did I have to do anything to install the module that I missed?

Avatar
Billiam

Try checking out the flux website and getting the copy of the dispatcher from there under 'Creating a Dispatcher'. https://facebook.github.io/flux/docs/todo-list.html. It requires installation of the es6-promise module and object-assign, but I've found so far object-assign is the easiest way to get around the merge requirement since merge has been deprecated since this video was released.

In reply to Fareez
Avatar
Billy

I was getting the same issue. This worked for me.
npm install merge
var merge = require('merge');

In reply to Fareez
Avatar
Billy

If you're getting this error - "module "react/lib/merge" try doing this.
npm install merge
var merge = require('merge');

Avatar
Billy

If you're getting this error - "module "react/lib/merge" try doing this.
npm install merge
var merge = require('merge');

Avatar
Victor

Joe, one of the nice process related things about this course are the analysis & design wireframes, not only the interface mock-up but also the one showing what still needs to be implemented. Are you using any particular application to create these diagrams? Thanks for a great, up-to-date and very clear video series!

Avatar
Rahul

how does 'this.setState()' inside 'onChange' work?
_onChange is passed to eventEmmiter. So won't 'this' point to a wrong object when '
onChange' is called by eventEmmiter? or am i missing something here?

Now that we have our store built, we can begin building our components. I don't know if you know this about me, but I have tremendous design abilities. This is a wire frame that I put together, and what we've got up here is our catalog component. We've got the title of our item, the cost of our item, and this add to cart component.

In our cart here, we've got the ability to remove an item, increase and decrease the quantity, and see our totals. Based on that, we can go ahead and get started building our components.

In a previous video, we took a look at how app actions is going to work. What I'm going to do here is I'm going to copy this old guy right here and create a new component. This is going to be our app add to cart component.

I'm going to paste that right there, and let's see. We're going to change this to add to cart, do the same on our export. Rather than passing in a string, it's going to be this prop's item, and let's see. This is going to be a button, and it's in our HTML. It's going to be add to cart.

This is just part of the beauty of Flux. We didn't. Have to inherit anything, we just created our quick little component. The only thing it actually inherited is the item that is going to get past him, otherwise, everything came from app actions. We are going to copy this guy once again. We're going to create a new component here.

This is going to be our App.RemoveFrom part component, paste this guy in here. Our component is going to be called "remove from cart." Here, rather than add item, we're going to remove item. In this case, rather than the item itself, we are going to be passing on the index, and we'll just go ahead and update this to an X.

Again, we're just going to copy this exact same code here. Create our app IncreaseItem.js, paste in again. This will be increase item, and let's see.

Here, we're going to say increase, and here, we'll just do a little plus symbol. One more time, copy this guy, create our App.Decrease item. Decrease, decrease, a little minus symbol there, exporting decrease item.

We just created all four of those components in record time. We didn't have to worry about any functionality being passed down to it. Again, this is part of the beauty of the Flux application architecture. The next component we are going to create is our catalog component.

That's going to be App-Catalog.js. If you take a look at our wire frame, this is going to be this section up here. We're going to have a the item here, and we're going to have this table with each of our items and our add to cart component. To build that up, we're actually going to go ahead and drop in our requirements, which are react, our app store, and our add to cart component.

Then, you create a function here called "get catalog." This is going to return our initial state. It's not going to return an object. If you have a key of items, the value of those is going to be AppStore.GetCatalog. Let's go ahead and build up our component here, called "catalog."

When we're all done, we're going to expert that, of course. Our get initial state, we're simply returning our get catalog method from above. In our return, we're going to paste this in. We are returning a table, we've got a little bootstrap class on the table, and a table hover.

Then, we've got the subject called items, which we are going to create right now. Our items equals This.State.Items.Map picks in each item. We're going to return a table row, that's TR, and we're going to have one, two, three table dialogs. Let's go to get those in there.

Our first one is going to have our Item.Title. Our second one is going to have our Item.Class. We'll put a little dollar sign right at the beginning of that. Our third one is going to be our, "add to cart," component and it's key of item is going to be equal to the item. We go ahead and save that guy.

Now, in our app component, let's say, we don't need this handler any longer. What we do need, rather than app actions, is our catalog component. Then, what we're going to return, something that looks like our wire frame. Let's wrap everything in a div, drop an H1 here. It says, "Let's shop." Below that, we're going to drop our catalog component.

Now, if we jump over to our application, reload this guy, you can see we've got all of our widgets in our catalog. If we want to bring up our console, we can see that in fact each time we click on this, we are sending our object to your dispatcher.

Now, we are getting a message here that we need to have a unique key, here on our catalog items right here, we're going to set a key equal to Item.ID. That should resolve that issue. There we go. We can see that each item is carrying over the item, then, we can see we've got our cost and our title, it's actually passing the item.

Now, we need to create our cart, which is very similar to our catalog, go ahead and copy that. Create a new component here called "AppPart.js." Paste this guy in there. In our cart, we're going to need react. We're also going to need the App Store, but we are not going to need add to cart. We're going to need remove from cart.

We're going to need are increase and decrease component. Rather than get catalog, we're going to need our cart items. This will return get cart. In our initial state, rather than get catalog, we're going to be calling cart items.

Unlike our catalog component, this component needs to actually be aware of changes in our application. What we're going to do is we're going to say, component will mount, and right now, we're going to use our AppStore.Addchange listener. We're going to call it This.Unchange.

We're going to go ahead and create that method right now. Here, what we're going to do is set our state to cart items. What we're really saying is any time something changes, we're going to get our updated state of our cart items and make it the state of our cart component.

Here in our render method, we've already got items, so we can reuse some of this. We're going to need to calculate our totals here. I'm going to say var total equals zero, get us started. While we're living through this, we're going to go ahead and calculate our subtotal and use that to increment our total.

Now here in our return, for the sake of brevity, I'm going to paste this in and we'll take a quick look at it. Again, we are returning a table row. We've got in the first column, we've got our remove from cart, and we're testing in our index, which we need to get in our mapping method right there.

Then we've got our title, our quantity, and then we've got our increase and decrease components, and then finally we've got our subtotal. Now here, in the return of our component, again, I'm going to paste this in and take a quick look at it. We're returning a table with a little bootstrap. We've got a header, and our table header, we've got an empty one here which is for our remove component.

We've got our item, which will be the title or quantity. We've got another one here for increase and decrease, and then, we've got our subtotal.

We drop our items into the table body, and then in the footer, we've got a table dialog here with a call span of four and a classing of text right just to get that word total all the way over to the right, and then, we output our total.

Go ahead and save that. We'll jump over here to our app, and drop in an H1 here in the cart. Then, we're going to need our cart component, and I'll try this guy out. Let's reload our app in the browser. Oh, cart items is not defined. That should be asked right there.

One more time, cool. We have our cart, we can add items to it, I can have a few of these guys. We'll increase the values here, and we will remove a couple of those items. This is widget A. Using our current app store, we can go ahead and add that a bunch of times and we're looking good.

One thing I noticed as I have this variable called catalog still in our cart component, I'm going to go ahead and update that to export cart. That's a minor tweak there, and everything seems to be working as expected.

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