⚠️ This lesson is retired and might contain outdated information.

Building a React.js App - ES6 Refactor: Routing without Mixins

Tyler McGinnis
InstructorTyler McGinnis
Share this video with your friends

Social Share Links

Send Tweet
Published 9 years ago
Updated 6 years ago

In this video, we’ll walk through refactoring the routing of our application to get away from mixins and be compatible with JavaScript Classes through context.

We only have four more components left until our entire app has been ES6-ified, so what we're going to do real quick is take care of this Home component since it's really small.

Import React from React, and then let's go ahead and create a class home which extends react.component, so just the same stuff we've been doing. Have a render method which returns this object, and then let's go ahead and export default home, so nothing new yet.

Now let's head over to the main.js file. Now, like usual, let's import react from react. Now let's create our main class, which extends react component, and then we're going to export default main. Once we do that, let's add our render method, which is going to return us the UI for this component.

The last component we're going to modify in this video is the search GitHub component, so same thing. We're going to do this. You should be very used to this by now. We're going to create a class of search GitHub, which extends react.component, and then we're going to export default search GitHub.

Let's bring these three functions up here. Let's get rid of the function, and let's get rid of these commas.

If you'll remember, functions inside of classes with react don't get auto-bounded, or this keyword doesn't get auto-bound. What we're going to have to do is make these arrow functions, which then invoke the specific function on the instance, and with GitRef, we're going to pass it ref.

This looks really good. Let's change this to const really quick. Perfect.

Now what you'll notice is everything is good except for this guy right here. If you remember correctly, what the router.history mixin is doing is it's taking our instance and it's mixing a few methods onto it. One of those methods is history, so we can call this .history.pushdate.

Because we no longer have that mixin, this .history's going to be undefined. We need to find another way to get history into this search GitHub component.

If you check out our routes, what React Router does is if you have a component that's being handled by the router, that component will receive certain properties and certain methods from the router. The problem is search GitHub isn't being handled by the router.

But we know that main is, and main is rendering search GitHub, so what if we do this? What if we say history is going to be this .props.history because React Router is going to pass main a history method?

Then what we can do is in search GitHub, let's add some prop types of history, which is going to be react.proptypes.object, and it's going to be required. Then all we need to do is change this .history to this .props.pushdate, so let's see if this works, and it does.

To recap, because we can't use mixins with react, we have to figure out another way to get history into this component. We do that by passing history from the main component into search GitHub as props, and the reason that the main component has access to this .props.history is because the main component is being controlled by the router.

Logan
Logan
~ 9 years ago

So thankful you covered this. During the react router section I kept worrying I was going to have to give up my nice ES6 so I could use the router mixin.

Tyler McGinnis
Tyler McGinnisinstructor
~ 9 years ago

Thanks Logan! You'll be happy to hear that React Router 1.0 (coming soon) gets rid of Mixins and you don't even have to use contexts. It's really slick.

Alex
Alex
~ 9 years ago

I am curios about debugger use. I like the idea of looking through the state but is there a way to do tracing instead (besides the obvious logging)?

So, I do not have to stop the world to examine things. It would be very useful for async/timed events. I know it is probably the wrong place to ask but maybe you can give me some references if not the answer.

Tyler McGinnis
Tyler McGinnisinstructor
~ 9 years ago

Hi Alex. Great question. I'm actually not sure since I always just use debugger. If you find an answer, definitely let me know.

egghead eggo
egghead eggo
~ 8 years ago

The lesson video has been updated!

Brandon
Brandon
~ 7 years ago

Hey Tyler, for some reason I'm doing the above and this.history, when passed from my parent component to the child component that needs them its undefined...even though the parent component is defined in routes.js.

When I console.log "this" in my parent component I see the props are defined after I open up the object in the console...and history is there. But right after I print JSON.stringify(this.props.history) and its an empty object. Any reason for this?

Brandon
Brandon
~ 7 years ago

Even adding console.log(this.props.history) in main before my render method console.logs to Object{} but when I actually open the object in the console, I see history there.

Markdown supported.
Become a member to join the discussionEnroll Today