Skip to content

Commit fe66c27

Browse files
committed
edit readme, add data load hooks
1 parent 590da7f commit fe66c27

File tree

8 files changed

+151
-11
lines changed

8 files changed

+151
-11
lines changed

README.md

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,32 @@
1-
# React Starterkit
1+
# React Starter
22

33
[![Dependabot badge](https://flat.badgen.net/dependabot/wbkd/react-starter?icon=dependabot)](https://dependabot.com/)
44

5-
**A minimal starting point for your react projects including:**
5+
A minimal starting point for interactive applications that we build at [webkid](https://webkid.io). The boilerplate already includes some basic components as well as hooks and utils that we are often using in our projects.
6+
7+
The starterkit is based on these libraries:
68

79
- [react](https://facebook.github.io/react/)
810
- [easy-peasy](https://easy-peasy.now.sh/)
911
- [emotion](https://emotion.sh/docs/styled)
1012
- [rebass](https://rebassjs.org/)
1113

12-
## Get the kit
14+
## Getting started
15+
16+
**[Create a new Github repository with the template](https://github.com/wbkd/react-starter/generate)** or clone the repo:
1317

1418
```sh
1519
git clone [email protected]:wbkd/react-starter.git && cd react-starter
1620
```
1721

18-
## Installation
22+
then install the dependencies via [npm](https://npmjs.org):
1923

2024
```sh
2125
npm install
2226
```
2327

28+
you are now ready to develop your app.
29+
2430
## Development
2531

2632
Builds the application and starts a webserver with hot loading.
@@ -38,6 +44,4 @@ Builds a minified version of the application in the build folder.
3844
npm run build
3945
```
4046

41-
## Contribute
42-
43-
Feel free to dive in! [Open an issue](https://github.com/wbkd/react-starter/issues/new) or submit a [pull request](https://github.com/wbkd/react-starter/pulls/).
47+
Additionally, a zipped version of the bundle is added as `build.zip`. We often use this for our clients to upload the application to their own servers.

package-lock.json

Lines changed: 38 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"config": {
44
"appname": "my-app-name"
55
},
6-
"version": "6.0.1",
6+
"version": "7.0.0",
77
"description": "lightweight react starterkit including webpack, unistore, styled components",
88
"scripts": {
99
"start": "webpack-dev-server --config webpack/webpack.config.dev.js",
@@ -59,12 +59,14 @@
5959
"@emotion/core": "^10.0.35",
6060
"@emotion/styled": "^10.0.27",
6161
"core-js": "^3.6.5",
62+
"d3-dsv": "^2.0.0",
6263
"easy-peasy": "^3.3.1",
6364
"emotion-normalize": "^10.1.0",
6465
"emotion-theming": "^10.0.27",
6566
"react": "^16.13.1",
6667
"react-dom": "^16.13.1",
6768
"rebass": "^4.0.7",
69+
"topojson-client": "^3.1.0",
6870
"unfetch": "^4.2.0"
6971
}
7072
}

src/hooks/useCsvData.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { useState, useEffect } from 'react';
2+
import { dsvFormat } from 'd3-dsv';
3+
import { fetchCsv } from 'utils/data-utils';
4+
5+
const defaultOptions = { delimiter: ',', onEachRow: (d) => d };
6+
7+
export default function useCsvData(url, options = defaultOptions) {
8+
const [data, setData] = useState(null);
9+
const mergedOptions = { ...defaultOptions, ...options };
10+
const { delimiter, onEachRow } = mergedOptions;
11+
const csvParser = dsvFormat(delimiter);
12+
13+
useEffect(() => {
14+
const loadData = async () => {
15+
const csv = await fetchCsv(url);
16+
const result = csvParser.parse(csv, onEachRow);
17+
setData(result);
18+
};
19+
20+
loadData();
21+
}, [url]);
22+
23+
return {
24+
data,
25+
isLoading: !data,
26+
};
27+
}

src/hooks/useJsonData.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { useState, useEffect } from 'react';
2+
import { fetchJson } from 'utils/data-utils';
3+
4+
export default function useJsonData(url) {
5+
const [data, setData] = useState(null);
6+
7+
useEffect(() => {
8+
const loadData = async () => {
9+
const result = await fetchJson(url);
10+
setData(result);
11+
};
12+
13+
loadData();
14+
}, [url]);
15+
16+
return {
17+
data,
18+
isLoading: !data,
19+
};
20+
}

src/hooks/useTopoData.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { useState, useEffect } from 'react';
2+
import { feature } from 'topojson-client';
3+
import { fetchJson } from 'utils/data-utils';
4+
5+
export default function useTopoData(url) {
6+
const [data, setData] = useState(null);
7+
8+
useEffect(() => {
9+
const loadData = async () => {
10+
const topo = await fetchJson(url);
11+
const topoKey = Object.keys(topo.objects)[0];
12+
const topoData = feature(topo, topo.objects[topoKey]);
13+
setData(topoData);
14+
};
15+
16+
loadData();
17+
}, [url]);
18+
19+
return {
20+
data,
21+
isLoading: !data,
22+
};
23+
}

src/utils/data-utils.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import fetch from 'unfetch';
2+
3+
export async function fetchJson(url) {
4+
try {
5+
const response = await fetch(url);
6+
const json = await response.json();
7+
return json;
8+
} catch (err) {
9+
console.log('Error loading data', err);
10+
return null;
11+
}
12+
}
13+
14+
export async function fetchCsv(url) {
15+
try {
16+
const response = await fetch(url);
17+
const csv = await response.text();
18+
return csv;
19+
} catch (err) {
20+
console.log('Error loading data', err);
21+
return null;
22+
}
23+
}
24+
25+
export default {
26+
fetchJson,
27+
fetchCsv,
28+
};

webpack/webpack.config.dev.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ module.exports = merge(common, {
1515
contentBase: Path.resolve(__dirname, 'build'),
1616
historyApiFallback: true,
1717
hot: true,
18+
host: '0.0.0.0',
1819
},
1920
plugins: [
2021
new Webpack.DefinePlugin({

0 commit comments

Comments
 (0)