-
Notifications
You must be signed in to change notification settings - Fork 1
/
App.js
46 lines (40 loc) · 1.37 KB
/
App.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import React from 'react';
// eslint-disable-next-line import/no-extraneous-dependencies
import ReactDOM from 'react-dom';
import indexHtml from './index.html';
import Wrangle from '../../src/Wrangle';
import WrangledCounter from './WrangledCounter';
// use onMissingPaths to invoke side-effects
// such as data fetching
// when requested paths are not present in state
function onMissingPaths(store, missingPaths) {
// setTimeout used to simulate an async data fetch
setTimeout(() => {
const pathValues = missingPaths.reduce((acc, path) => {
const localValue = localStorage.getItem(path);
const parsedValue = localValue === null ? 0 : JSON.parse(localValue);
acc[path] = parsedValue;
return acc;
}, {});
store.setPaths(pathValues);
}, 1500);
}
// use onStoreChange to invoke side-effects
// such as persisting values when path values change
function onStoreChange(store, changedPaths) {
Object.keys(changedPaths).forEach((path) => {
const pathValue = changedPaths[path];
localStorage.setItem(path, JSON.stringify(pathValue));
});
}
function App() {
// the root level component needs to be wrapped in <Wrangle />
return (
<Wrangle debug={true}
onMissingPaths={onMissingPaths}
onStoreChange={onStoreChange}>
<WrangledCounter />
</Wrangle>
);
}
ReactDOM.render(<App />, document.getElementById('app'));