Skip to content

Commit

Permalink
Add async example
Browse files Browse the repository at this point in the history
  • Loading branch information
badsyntax committed Oct 4, 2017
1 parent da1fed3 commit 1c8c757
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 20 deletions.
30 changes: 15 additions & 15 deletions package-lock.json → npm-shrinkwrap.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@
"collapseWhitespace": false
}
}
}
}
6 changes: 6 additions & 0 deletions public/data.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"items": [
"async loaded item1",
"async loaded item2"
]
}
28 changes: 24 additions & 4 deletions src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,42 @@ import logo from "./logo.svg";
import "./App.css";
import { BrowserRouter, Link, Route } from "react-router-dom";

const About = () =>
const About = () => (
<div>
<h2>About</h2>
<p className="App-intro">
This is the about page, go to the <Link to="/">home page</Link>?
</p>
</div>;
</div>
);

const Home = () =>
const Home = () => (
<div>
<h2>Home</h2>
<p className="App-intro">
This is the home page, go to the <Link to="/about/">about page</Link>?
</p>
</div>;
</div>
);

class App extends Component {
constructor() {
super();
this.state = {
items: []
};
}

async componentDidMount() {
fetch("/data.json")
.then(res => res.json())
.then(data => {
this.setState({
items: data.items
});
});
}

render() {
return (
<BrowserRouter>
Expand All @@ -30,6 +49,7 @@ class App extends Component {
</header>
<Route path="/" exact component={Home} />
<Route path="/about/" component={About} />
{this.state.items.map(item => <div key={item}>{item}</div>)}
</div>
</BrowserRouter>
);
Expand Down

0 comments on commit 1c8c757

Please sign in to comment.