Course webpage for INFO 253 at the School of Information
Your task is to take the website you have been building into React
Note: PLEASE CREATE A FOLDER CALLED “assignment4” in your assignment repo that you created
This assignment will require that you need figure out how to route url paths (e.g. localhost:3000/aboutus) to render specific react components. There are several ways to do this, many beyond what we have covered at this point. However, here’s a simple way of achieving this goal using the elements of React that we covered and doing some search within JavaScript:
import React from 'react';
import './App.css';
// Defining a quick Header component. Note that it has two links: / for the home page and /page2 for another page
class Header extends React.Component {
render() {
return (
<div>
<h1>Header</h1>
<a href="/">Homepage</a><br />
<a href="/page2">Page2</a>
</div>
)
}
}
// Defining a home page component. This component will contain content specific to the home page
class HomePage extends React.Component {
render() {
return (
<h1>HomePage</h1>
)
}
}
// Defining a Page2 Component. This component will contain content specific to whatever Page2 is
class Page2 extends React.Component {
render() {
return (
<h1>Page2</h1>
)
}
}
// Quick Footer component
class Footer extends React.Component {
render() {
return (
<h1>Footer</h1>
)
}
}
// This is the main Component that index.js is going to render. This is responsible for rendering the correct components for each given
// Page
class App extends React.Component {
constructor(props) {
super(props)
// here's the special sauce: window.location.pathname gives you the path of the current url outside of the domain.
// For example, if your current url is "localhost:3000/page2", window.location.pathname will equal "/page2"
this.state = {'currentPage': window.location.pathname }
}
// This render function now looks at the current state (set by window.location.pathname), and renders the correct components
render() {
// If there's no path, render the home page (i.e. http://localhost:3000/)
if (this.state.currentPage === "/") {
return (
<div>
<Header />
<HomePage />
<Footer />
</div>
)
}
// If the path is /page2 (i.e. http://localhost:3000/page2, then render the page with Page2 content)
else if (this.state.currentPage === "/page2") {
return (
<div>
<Header />
<Page2 />
<Footer />
</div>
)
}
}
}
Please note that this is not the most efficient routing method since we are reloading the page on every click and there are ways to prevent our need to do so. However this way uses what you learned so far.
Also note: window.location.pathname
is pure JavaScript, not React. Actually the entire “window” object is pure JavaScript and is available within any browser.