Skip to content

Commit 4e420fa

Browse files
Andrii Ughengeveld
authored andcommitted
added example for using react-async with react-router (#26)
1 parent ddf7c8e commit 4e420fa

File tree

11 files changed

+142
-0
lines changed

11 files changed

+142
-0
lines changed

examples/react-async-router/.babelrc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"presets": ["@babel/preset-react"]
3+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.cache
2+
dist

examples/react-async-router/README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
## An example of using `react-async` with `react-router` (built with parcel)
2+
3+
The idea was to make fetching data for pages (components) configurable in a declarative way
4+
5+
```
6+
<Router>
7+
...
8+
<ApiRouter path="/repositories" fetchUrl='https://api.github.com/repositories' component={RepositoriesComponent} />
9+
</Router>
10+
```
11+
12+
## Running the example
13+
14+
```
15+
npm install
16+
npm run start
17+
```
18+
and then goto [http://localhost:1234](http://localhost:123)
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<html>
2+
<head>
3+
<style>
4+
a {
5+
margin-right: 1em;
6+
}
7+
</style>
8+
</head>
9+
<body>
10+
<script src="./index.js"></script>
11+
<div id="app"></div>
12+
</body>
13+
</html>

examples/react-async-router/index.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import React from 'react';
2+
import ReactDOM from 'react-dom';
3+
import { BrowserRouter as Router, Route } from "react-router-dom";
4+
import ApiRouter from './js/ApiRouter';
5+
import ContributorsComponent from './js/ContributorsComponent';
6+
import RepositoriesComponent from './js/RepositoriesComponent';
7+
import Header from './js/Header';
8+
9+
const App = () => (
10+
<Router>
11+
<React.Fragment>
12+
<Header/>
13+
<ApiRouter exact path="/" fetchUrl='https://api.github.com/repos/ghengeveld/react-async/contributors' component={ContributorsComponent} />
14+
<ApiRouter path="/repositories" fetchUrl='https://api.github.com/repositories' component={RepositoriesComponent} />
15+
</React.Fragment>
16+
</Router>)
17+
18+
document.addEventListener('DOMContentLoaded', () => {
19+
ReactDOM.render(<App />, document.getElementById('app'));
20+
});

examples/react-async-router/js/Api.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import React from 'react';
2+
import Async from 'react-async';
3+
4+
const loader = (fetchUrl) =>
5+
fetch(fetchUrl)
6+
.then(res => (res.ok ? res : Promise.reject(res)))
7+
.then(res => res.json())
8+
9+
const Api = ( {fetchUrl, children}) => (
10+
<Async promiseFn={() => loader(fetchUrl)}>
11+
{({ data, error, isLoading}) => {
12+
const childrenWithProps = React.Children.map(children, child =>
13+
React.cloneElement(child, { data, error, isLoading })
14+
);
15+
return (<div>{childrenWithProps}</div>)
16+
}}
17+
</Async>
18+
)
19+
20+
export default Api;
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import React from 'react';
2+
import { Route} from "react-router-dom";
3+
import Api from './Api';
4+
5+
const ApiRouter = (props) => {
6+
const Child = props.component;
7+
const c = () => (
8+
<Api fetchUrl={props.fetchUrl}>
9+
<Child {...props} />
10+
</Api>
11+
);
12+
return (<Route {...props} component={c} />);
13+
}
14+
15+
export default ApiRouter;
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import React from 'react';
2+
3+
const ContributorsComponent = ({data, error, isLoading}) => {
4+
if (isLoading) return 'Loading Contributers...'
5+
if (error) return 'Error'
6+
return (<ul>{data.map(e => (<li key={e.id}>{e.login}</li>))}</ul>)
7+
}
8+
9+
export default ContributorsComponent;
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import React from 'react';
2+
import { NavLink } from 'react-router-dom'
3+
4+
const Header = () => (
5+
<div>
6+
<NavLink to="/">Contributors To react-async</NavLink>
7+
<NavLink to="/repositories">Other github repositories</NavLink>
8+
</div>);
9+
10+
export default Header;
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import React from 'react';
2+
3+
const RepositoriesComponent = ({data, error, isLoading}) => {
4+
if (isLoading) return 'Loading Repositories...'
5+
if (error) return 'Error'
6+
return (<ul>{data.map(e => (<li key={e.id}>{e.name}</li>))}</ul>)
7+
}
8+
9+
export default RepositoriesComponent;

0 commit comments

Comments
 (0)