-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathExample.tsx
68 lines (61 loc) · 1.43 KB
/
Example.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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import createZustandConstate from './zustand-constate';
import * as React from 'react';
import {Button} from 'react-native';
import {useEffect, useState} from 'react';
import shallow from 'zustand/shallow';
interface Props {
step: number;
}
type State = {
count: number;
increase: () => void;
runTimes: number;
} & Props;
const {Provider, useStore} = createZustandConstate<State, Props>(
(set) => ({
count: 0,
step: 1,
runTimes: 0,
increase: () => set((state) => ({count: state.count + state.step})),
}),
({useStore}) => {
const [runTimes, setRunTimes] = useState(0);
const increase = useStore((state) => state.increase);
// auto increase every 3 seconds
useEffect(() => {
const interval = setInterval(() => {
setRunTimes((r) => r + 1);
increase();
}, 3000);
return () => {
clearInterval(interval);
};
}, []);
return {runTimes};
},
);
const TheButton = () => {
const [count, step, increase, runnedTimes] = useStore(
(state) => [state.count, state.step, state.increase, state.runTimes],
shallow,
);
return (
<Button
title={`Count/Step/Runned: ${count}/${step}/${runnedTimes}`}
onPress={increase}
/>
);
};
const Example = () => {
return (
<>
<Provider step={1}>
<TheButton />
</Provider>
<Provider step={2}>
<TheButton />
</Provider>
</>
);
};
export default Example;