Reader small image

You're reading from  React Components

Product typeBook
Published inApr 2016
Publisher
ISBN-139781785889288
Edition1st Edition
Tools
Right arrow
Author (1)
Christopher Pitt
Christopher Pitt
author image
Christopher Pitt

Christopher Pitt is a principal developer for SilverStripe in Wellington, New Zealand. He usually works on open source software, though sometimes you'll find him building compilers and robots.
Read more about Christopher Pitt

Right arrow

Chapter 6. Changing Views

In the previous chapter, we covered a bit about material design, and as a result, we split the login and page admin sections into different files. We stopped short of making the login redirect us to the page admin section.

In this chapter, you will learn how to change sections without reloading the page. We'll use this knowledge to create public pages for the website our CMS is meant to control.

We'll see how to work with the browser's address bar and location history. We'll also learn how to use popular libraries to abstract these things for us, so we can save time on writing boilerplate and concentrate on making our interfaces great!

Location, location, location!


Before we can learn about alternatives to reloading pages, let's take a look at how the browser manages reloads.

You've probably encountered the window object. It's a global catch-all object for browser-based functionality and state. It's also the default this scope in any HTML page:

We've even accessed window earlier. When we rendered to document.body or used document.querySelector, these properties and methods were called on the window object. It's the same as calling window.document.querySelector.

Most of the time, document is the only property we need. That doesn't mean it's the only property useful to us. Try the following in the console:

console.log(window.location);

You should see something similar to the following:

Location {
    hash: ""
    host: "127.0.0.1:3000"
    hostname: "127.0.0.1"
    href: "http://127.0.0.1:3000/examples/login.html"
    origin: "http://127.0.0.1:3000"
    pathname: "/examples/login.html"
    port: "3000"
    ...
}

If we were trying...

Using a router


Our hash code is functional but invasive. We shouldn't call the render method from inside a component (at least not the one we own). So, we will instead use a popular router to manage this stuff for us.

Download the router with the following command:

$ npm install react-router --save

Then, we need to put login.html and page-admin.html back into the same file:

<!DOCTYPE html>
<html>
    <head>
        <script src="/node_modules/babel-core/browser.js"></script>
        <script src="/node_modules/systemjs/dist/system.js"></script>
        <script src="https://storage.googleapis.com/code.getmdl.io/1.0.6/material.min.js"></script>
        <link rel="stylesheet" href="https://storage.googleapis.com/code.getmdl.io/1.0.6/material.indigo-pink.min.css" />
        <link rel="stylesheet" href="https://fonts.googleapis.com/icon?family=Material+Icons" />
        <link rel="stylesheet" href="admin.css" />
    </head...

Creating public pages


Now that we can easily switch between CMS sections, we can use the same trick to show the public pages of our website. Let's create a new HTML page just for these:

<!DOCTYPE html>
<html>
    <head>
        <script src="/node_modules/babel-core/browser.js"></script>
        <script src="/node_modules/systemjs/dist/system.js"></script>
    </head>
    <body>
        <div class="react"></div>
        <script>
            System.config({
                "transpiler": "babel",
                "map": {
                    "react": "/examples/react/react",
                    "react-dom": "/examples/react/react-dom",
                    "router": "/node_modules/react-router/umd/ReactRouter"
                },
                "baseURL": "../",
                "defaultJSExtensions": true
            });

            System.import("examples/index");
        </script>
    </body>
</html>

This...

Summary


In this chapter, you learned about how the browser stores URL history, and how we can manipulate it to load different sections without full page reloads. It introduced a bit of complexity, but we also saw that there are other alternatives (for example, the hashchange event) that reduce the complexity while still reducing the number of full page reloads we need to perform.

You also learned about a popular React router and used it to abstract the manual location tracking or changing we had to do before.

In the next chapter, you'll learn about server-side rendering and application structure.

lock icon
The rest of the chapter is locked
You have been reading a chapter from
React Components
Published in: Apr 2016Publisher: ISBN-13: 9781785889288
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Christopher Pitt

Christopher Pitt is a principal developer for SilverStripe in Wellington, New Zealand. He usually works on open source software, though sometimes you'll find him building compilers and robots.
Read more about Christopher Pitt