Skip to content

Instantly share code, notes, and snippets.

@jameslaneconkling
Created July 18, 2017 15:44
Show Gist options
  • Save jameslaneconkling/74d3eb2f8834abc62640ac6466341d51 to your computer and use it in GitHub Desktop.
Save jameslaneconkling/74d3eb2f8834abc62640ac6466341d51 to your computer and use it in GitHub Desktop.
React-Router@5 Template
<div id="root">
</div>
const { render } = ReactDOM
const {
HashRouter,
Route,
Link
} = ReactRouterDOM
const App = () => (
<HashRouter>
<div>
<AddressBar/>
<Route exact path="/" component={Home}/>
<Route path="/items/:itemId" component={Item}/>
</div>
</HashRouter>
)
const Home = () => (
<div>
<h1>demo</h1>
<Link to="/items/1">Go to Item 1</Link>
</div>
)
const Item = (props) => (
<div>
<h3>item #{props.match.params.itemId}</h3>
<Link to="/items/2">Go to Item 2 (works!)</Link>
<br />
<button
onClick={() => {
// console.log('history', props.history.push);
props.history.push("/items/2")
}}
>
Go to Item 2 (doesn't work)
</button>
</div>
)
const AddressBar = () => (
<Route render={({ location: { pathname }, goBack, goForward }) => (
<div className="address-bar">
<div>
<button
className="ab-button"
onClick={goBack}
>◀︎</button>
</div>
<div>
<button
className="ab-button"
onClick={goForward}
>▶</button>
</div>
<div className="url">URL: {pathname}</div>
</div>
)}/>
)
render(<App/>, document.getElementById('root'))
<script src="https://unpkg.com/react@15.4.2/dist/react.js"></script>
<script src="https://unpkg.com/react-dom@15.4.2/dist/react-dom.js"></script>
<script src="https://unpkg.com/react-router-dom@4.1.2/umd/react-router-dom.js"></script>
body {
margin: 8px;
font-family: -apple-system,
BlinkMacSystemFont,
"Segoe UI",
Roboto,
Oxygen,
Ubuntu,
Cantarell,
"Open Sans",
"Helvetica Neue",
sans-serif;
}
.address-bar {
margin: -8px;
padding: 10px;
background: #ccc;
display: flex;
align-items: center;
}
.ab-button {
border: none;
background: none;
font-size: 30px;
color: #444;
outline: none;
}
.ab-button:focus {
color: hsl(200, 50%, 50%)
}
.ab-button:hover {
color: #666;
}
.ab-button:active {
color: #000;
}
.url {
background: white;
flex: 1;
margin-left: 10px;
padding: 10px;
color: #aaa;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment