-
Notifications
You must be signed in to change notification settings - Fork 8
/
index.tsx
32 lines (27 loc) · 904 Bytes
/
index.tsx
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
import { number } from '@recoiljs/refine'
import { useRecoilState } from 'recoil'
import { syncEffect } from 'recoil-sync'
import { atomFamilyWithInitialValue } from 'recoil-sync-next'
import styles from './index.module.css'
export const counter = atomFamilyWithInitialValue<number, string>({
key: 'counterState',
effects: [syncEffect({ storeKey: 'url-json-store', refine: number() })],
})
export type Props = {
name: string
initialValue: number
}
export const Counter: React.FC<Props> = ({ name, initialValue }) => {
const [count, setCount] = useRecoilState(counter(name, initialValue))
return (
<div key={name} className={styles.wrapper}>
<div>
count[{name}]: {count}
</div>
<div>
<button onClick={() => setCount((prev) => prev + 1)}>+</button>
<button onClick={() => setCount((prev) => prev - 1)}>-</button>
</div>
</div>
)
}