Skip to content

Commit 4d7d34b

Browse files
committed
Cleanup and consistent code style.
1 parent 4e420fa commit 4d7d34b

File tree

8 files changed

+84
-61
lines changed

8 files changed

+84
-61
lines changed

examples/react-async-router/README.md

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
11
## An example of using `react-async` with `react-router` (built with parcel)
22

3-
The idea was to make fetching data for pages (components) configurable in a declarative way
3+
The idea is to make fetching data for pages (components) configurable in a declarative way.
44

55
```
6-
<Router>
7-
...
8-
<ApiRouter path="/repositories" fetchUrl='https://api.github.com/repositories' component={RepositoriesComponent} />
9-
</Router>
6+
<Router>
7+
...
8+
<ApiRouter path="/repositories" fetchUrl="https://api.github.com/repositories" component={RepositoriesComponent} />
9+
</Router>
1010
```
1111

1212
## Running the example
1313

1414
```
1515
npm install
16-
npm run start
16+
npm start
1717
```
18-
and then goto [http://localhost:1234](http://localhost:123)
18+
19+
Then visit [http://localhost:1234](http://localhost:1234).

examples/react-async-router/index.js

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,30 @@
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';
1+
import React from "react"
2+
import ReactDOM from "react-dom"
3+
import { BrowserRouter as Router } 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"
88

99
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>)
10+
<Router>
11+
<>
12+
<Header/>
13+
<ApiRouter
14+
exact
15+
path="/"
16+
fetchUrl="https://api.github.com/repos/ghengeveld/react-async/contributors"
17+
component={ContributorsComponent}
18+
/>
19+
<ApiRouter
20+
path="/repositories"
21+
fetchUrl="https://api.github.com/repositories"
22+
component={RepositoriesComponent}
23+
/>
24+
</>
25+
</Router>
26+
)
1727

18-
document.addEventListener('DOMContentLoaded', () => {
19-
ReactDOM.render(<App />, document.getElementById('app'));
20-
});
28+
document.addEventListener("DOMContentLoaded", () => {
29+
ReactDOM.render(<App />, document.getElementById("app"))
30+
})

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

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,22 @@
1-
import React from 'react';
2-
import Async from 'react-async';
1+
import React from "react"
2+
import Async from "react-async"
33

4-
const loader = (fetchUrl) =>
4+
const loader = fetchUrl =>
55
fetch(fetchUrl)
66
.then(res => (res.ok ? res : Promise.reject(res)))
77
.then(res => res.json())
88

9-
const Api = ( {fetchUrl, children}) => (
9+
const Api = ({ fetchUrl, children }) => (
1010
<Async promiseFn={() => loader(fetchUrl)}>
11-
{({ data, error, isLoading}) => {
11+
{({ data, error, isLoading }) => {
1212
const childrenWithProps = React.Children.map(children, child =>
1313
React.cloneElement(child, { data, error, isLoading })
14-
);
15-
return (<div>{childrenWithProps}</div>)
14+
)
15+
return (
16+
<div>{childrenWithProps}</div>
17+
)
1618
}}
1719
</Async>
1820
)
1921

20-
export default Api;
22+
export default Api
Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
import React from 'react';
2-
import { Route} from "react-router-dom";
3-
import Api from './Api';
1+
import React from "react"
2+
import { Route} from "react-router-dom"
3+
import Api from "./Api"
44

55
const ApiRouter = (props) => {
6-
const Child = props.component;
6+
const Child = props.component
77
const c = () => (
88
<Api fetchUrl={props.fetchUrl}>
99
<Child {...props} />
1010
</Api>
11-
);
12-
return (<Route {...props} component={c} />);
11+
)
12+
return (<Route {...props} component={c} />)
1313
}
1414

15-
export default ApiRouter;
15+
export default ApiRouter
Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
1-
import React from 'react';
1+
import React from "react"
22

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>)
3+
const ContributorsComponent = ({ data, error, isLoading }) => {
4+
if (isLoading) return "Loading Contributers..."
5+
if (error) return "Error"
6+
return (
7+
<ul>
8+
{data.map(e => (
9+
<li key={e.id}>{e.login}</li>
10+
))}
11+
</ul>
12+
)
713
}
814

9-
export default ContributorsComponent;
15+
export default ContributorsComponent
Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
import React from 'react';
2-
import { NavLink } from 'react-router-dom'
1+
import React from "react"
2+
import { NavLink } from "react-router-dom"
33

44
const Header = () => (
55
<div>
66
<NavLink to="/">Contributors To react-async</NavLink>
77
<NavLink to="/repositories">Other github repositories</NavLink>
8-
</div>);
8+
</div>
9+
)
910

10-
export default Header;
11+
export default Header
Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
1-
import React from 'react';
1+
import React from "react"
22

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>)
3+
const RepositoriesComponent = ({ data, error, isLoading }) => {
4+
if (isLoading) return "Loading Repositories..."
5+
if (error) return "Error"
6+
return (
7+
<ul>
8+
{data.map(e => (
9+
<li key={e.id}>{e.name}</li>
10+
))}
11+
</ul>
12+
)
713
}
814

9-
export default RepositoriesComponent;
15+
export default RepositoriesComponent

examples/react-async-router/package.json

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
11
{
22
"name": "react-async-router",
3-
"version": "1.0.0",
4-
"description": "",
3+
"version": "0.0.0",
4+
"private": true,
55
"main": "index.js",
66
"scripts": {
7-
"start": "parcel index.html",
8-
"test": "echo \"Error: no test specified\" && exit 1"
7+
"start": "parcel index.html"
98
},
10-
"author": "",
11-
"license": "ISC",
129
"dependencies": {
1310
"@babel/preset-react": "^7.0.0",
1411
"parcel-bundler": "^1.11.0",

0 commit comments

Comments
 (0)