Skip to content

Commit

Permalink
Scaffolding components with React Router (BrowserRouter/Link).
Browse files Browse the repository at this point in the history
  • Loading branch information
roelfie committed Jan 6, 2021
1 parent e6b76e9 commit d01cf45
Show file tree
Hide file tree
Showing 11 changed files with 209 additions and 75 deletions.
31 changes: 27 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1151,12 +1151,35 @@ Instead of passing in the userId to the UserHeader component, and make each comp
Features:
- React Router
- Authentication (OAuth)
- Forms
- CRUD
- React Router (section 20)
- Authentication (OAuth) (section 21)
- Forms (section 23)
- CRUD (section 24)
- Error handling
### Types of Routers
[ReactRouter](https://reactrouter.com/) comes with 3 types of routers. Which one to use depends on your deployment situation (what web server you use and how you can/want to configure it).
- BrowserRouter
- path `/page1` => url `https://host:port/page1`
- configure your web server to route all unknowns to `index.html`.
- HashRouter
- path `/page1` => url `https://host:port/#/page1`
- configure your web server to ignore everything behind `#`.
- MemoryRouter
- path `/page1` => url `https://host:port`
- URL is constant. Easy to deploy but no way to bookmark parts of your application.

#### Traditional web server vs. React Dev server

A React application is typically a single page application that is served by `/index.html`. Web servers automatically serve `index.html` if you request `/`. But what happens if you request a resource `/page2` that the server doesn't know of (NB: the React Router path `/page2` only has meaning inside the React application, which runs inside the browser)?
- Traditional web server will return HTTP 404 by default.
- React Dev web server is configured to return `/index.html`.
In other words, your bookmarks for specific pages inside your React app won't work, unless you configure your web server so that it routes those paths to `/index.html`.

# Appendix: JavaScript

### Named vs. default exports
Expand Down
70 changes: 0 additions & 70 deletions streams/client/README.md

This file was deleted.

96 changes: 96 additions & 0 deletions streams/client/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions streams/client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"@testing-library/user-event": "^12.6.0",
"react": "^17.0.1",
"react-dom": "^17.0.1",
"react-router-dom": "^5.2.0",
"react-scripts": "4.0.1",
"web-vitals": "^0.2.4"
},
Expand Down
22 changes: 21 additions & 1 deletion streams/client/src/components/App.jsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,28 @@
import React from "react";
import { BrowserRouter, Route } from "react-router-dom";
import StreamList from "./streams/StreamList";
import StreamShow from "./streams/StreamShow";
import StreamCreate from "./streams/StreamCreate";
import StreamEdit from "./streams/StreamEdit";
import StreamDelete from "./streams/StreamDelete";
import Header from "./Header";

class App extends React.Component {
render() {
return <div>Streams Client</div>;
return (
<div className='ui container'>
<BrowserRouter>
<div>
<Header />
<Route path='/' exact component={StreamList} />
<Route path='/streams/show' exact component={StreamShow} />
<Route path='/streams/edit' exact component={StreamEdit} />
<Route path='/streams/new' exact component={StreamCreate} />
<Route path='/streams/delete' exact component={StreamDelete} />
</div>
</BrowserRouter>
</div>
);
}
}

Expand Down
19 changes: 19 additions & 0 deletions streams/client/src/components/Header.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import React from "react";
import { Link } from "react-router-dom";

const Header = () => {
return (
<div className='ui secondary pointing menu'>
<Link to='/streams/new' className='item'>
Stream
</Link>
<div className='right menu'>
<Link to='/' className='item'>
All Streams
</Link>
</div>
</div>
);
};

export default Header;
9 changes: 9 additions & 0 deletions streams/client/src/components/streams/StreamCreate.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import React from "react";

class StreamCreate extends React.Component {
render() {
return <div>StreamCreate</div>;
}
}

export default StreamCreate;
9 changes: 9 additions & 0 deletions streams/client/src/components/streams/StreamDelete.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import React from "react";

class StreamDelete extends React.Component {
render() {
return <div>StreamDelete</div>;
}
}

export default StreamDelete;
9 changes: 9 additions & 0 deletions streams/client/src/components/streams/StreamEdit.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import React from "react";

class StreamEdit extends React.Component {
render() {
return <div>StreamEdit</div>;
}
}

export default StreamEdit;
9 changes: 9 additions & 0 deletions streams/client/src/components/streams/StreamList.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import React from "react";

class StreamList extends React.Component {
render() {
return <div>StreamList</div>;
}
}

export default StreamList;
9 changes: 9 additions & 0 deletions streams/client/src/components/streams/StreamShow.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import React from "react";

class StreamShow extends React.Component {
render() {
return <div>StreamShow</div>;
}
}

export default StreamShow;

0 comments on commit d01cf45

Please sign in to comment.