Skip to content

Commit

Permalink
examples: fix format, rename json to transit
Browse files Browse the repository at this point in the history
  • Loading branch information
akfm.sato committed Aug 1, 2022
1 parent e7d6164 commit 24796e3
Show file tree
Hide file tree
Showing 10 changed files with 42 additions and 40 deletions.
4 changes: 2 additions & 2 deletions examples/history-sync-transit/README.md
@@ -1,6 +1,6 @@
## history-sync-json
## history-sync-transit

Example of `RecoilHistorySyncJSONNext`.
Example of `RecoilHistorySyncTransitNext`.

```bash
# run example application
Expand Down
2 changes: 1 addition & 1 deletion examples/history-sync-transit/package.json
@@ -1,5 +1,5 @@
{
"name": "history-sync-json",
"name": "history-sync-transit",
"version": "0.1.0",
"private": true,
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion examples/history-sync-transit/pages/_app.tsx
Expand Up @@ -14,7 +14,7 @@ function MyApp({ Component, pageProps }: AppProps) {
{
tag: 'VS',
class: ViewState,
write: x => [x.active, x.pos],
write: (x) => [x.active, x.pos],
read: ([active, pos]) => new ViewState(active, pos),
},
]}
Expand Down
Expand Up @@ -3,4 +3,4 @@
justify-content: space-between;
width: 200px;
margin: 0;
}
}
Expand Up @@ -5,33 +5,34 @@ import { syncEffect } from 'recoil-sync'
import styles from './index.module.css'

export class ViewState {
constructor(
public active: boolean,
public pos: [number, number],
) {}
constructor(public active: boolean, public pos: [number, number]) {}
}

const viewStateChecker = custom(x => x instanceof ViewState ? x : null);
const viewStateChecker = custom((x) => (x instanceof ViewState ? x : null))
const viewState = atom<ViewState>({
key: 'viewState',
default: new ViewState(true, [1, 2]),
effects: [syncEffect({ storeKey: 'history-transit-store', refine: viewStateChecker })],
effects: [
syncEffect({ storeKey: 'history-transit-store', refine: viewStateChecker }),
],
})

export const ViewStateForm: React.FC = () => {
const [state, setState] = useRecoilState(viewState)
const toggleActive = () => setState(new ViewState(!state.active, state.pos))
const incrementPos = () => setState(new ViewState(state.active, [state.pos[0] + 1, state.pos[1] + 1]))
const incrementPos = () =>
setState(new ViewState(state.active, [state.pos[0] + 1, state.pos[1] + 1]))

return (
<div>
<div className={styles.row}>
active: <input type="checkbox" checked={state.active} onChange={toggleActive} />
</div>
<div className={styles.row}>
<div>pos: { JSON.stringify(state.pos) }</div>
<button onClick={incrementPos}>increment</button>
</div>
active:{' '}
<input type="checkbox" checked={state.active} onChange={toggleActive} />
</div>
<div className={styles.row}>
<div>pos: {JSON.stringify(state.pos)}</div>
<button onClick={incrementPos}>increment</button>
</div>
</div>
)
}
)
}
4 changes: 2 additions & 2 deletions examples/url-sync-transit/README.md
@@ -1,6 +1,6 @@
## url-sync-json
## url-sync-transit

Example of `RecoilURLSyncJSONNext`.
Example of `RecoilURLSyncTransitNext`.

```bash
# run example application
Expand Down
2 changes: 1 addition & 1 deletion examples/url-sync-transit/package.json
@@ -1,5 +1,5 @@
{
"name": "url-sync-json",
"name": "url-sync-transit",
"version": "0.1.0",
"private": true,
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion examples/url-sync-transit/pages/_app.tsx
Expand Up @@ -15,7 +15,7 @@ function MyApp({ Component, pageProps }: AppProps) {
{
tag: 'VS',
class: ViewState,
write: x => [x.active, x.pos],
write: (x) => [x.active, x.pos],
read: ([active, pos]) => new ViewState(active, pos),
},
]}
Expand Down
Expand Up @@ -3,4 +3,4 @@
justify-content: space-between;
width: 200px;
margin: 0;
}
}
31 changes: 16 additions & 15 deletions examples/url-sync-transit/src/components/ViewStateForm/index.tsx
Expand Up @@ -5,33 +5,34 @@ import { syncEffect } from 'recoil-sync'
import styles from './index.module.css'

export class ViewState {
constructor(
public active: boolean,
public pos: [number, number],
) {}
constructor(public active: boolean, public pos: [number, number]) {}
}

const viewStateChecker = custom(x => x instanceof ViewState ? x : null);
const viewStateChecker = custom((x) => (x instanceof ViewState ? x : null))
const viewState = atom<ViewState>({
key: 'viewState',
default: new ViewState(true, [1, 2]),
effects: [syncEffect({ storeKey: 'url-transit-store', refine: viewStateChecker })],
effects: [
syncEffect({ storeKey: 'url-transit-store', refine: viewStateChecker }),
],
})

export const ViewStateForm: React.FC = () => {
const [state, setState] = useRecoilState(viewState)
const toggleActive = () => setState(new ViewState(!state.active, state.pos))
const incrementPos = () => setState(new ViewState(state.active, [state.pos[0] + 1, state.pos[1] + 1]))
const incrementPos = () =>
setState(new ViewState(state.active, [state.pos[0] + 1, state.pos[1] + 1]))

return (
<div>
<div className={styles.row}>
active: <input type="checkbox" checked={state.active} onChange={toggleActive} />
</div>
<div className={styles.row}>
<div>pos: { JSON.stringify(state.pos) }</div>
<button onClick={incrementPos}>increment</button>
</div>
active:{' '}
<input type="checkbox" checked={state.active} onChange={toggleActive} />
</div>
<div className={styles.row}>
<div>pos: {JSON.stringify(state.pos)}</div>
<button onClick={incrementPos}>increment</button>
</div>
</div>
)
}
)
}

0 comments on commit 24796e3

Please sign in to comment.