-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ae04e93
commit 89d6023
Showing
8 changed files
with
99 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
npm i zustand |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { View, Text } from "react-native"; | ||
import { useCounterStore } from "../store/store"; | ||
|
||
const Index = () => { | ||
const count = useCounterStore((state) => state.count); | ||
|
||
return ( | ||
<View> | ||
<Text style={{ fontSize: 24 }}>Count: {count}</Text> | ||
</View> | ||
); | ||
}; | ||
|
||
export default Index; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { create } from "zustand"; | ||
|
||
type CounterStore = { | ||
count: number; | ||
}; | ||
|
||
export const useCounterStore = create<CounterStore>(() => ({ | ||
count: 0, | ||
})); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { View, Text, Button } from "react-native"; | ||
import { useCounterStore } from "../store/store"; | ||
|
||
const Index = () => { | ||
const count = useCounterStore((state) => state.count); | ||
const increment = useCounterStore((state) => state.increment); | ||
const decrement = useCounterStore((state) => state.decrement); | ||
|
||
return ( | ||
<View> | ||
<Text style={{ fontSize: 24 }}>Count: {count}</Text> | ||
<Button onPress={increment} title={"Increment"} /> | ||
<Button onPress={decrement} title={"Decrement"} /> | ||
</View> | ||
); | ||
}; | ||
|
||
export default Index; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { create } from "zustand"; | ||
|
||
type CounterStore = { | ||
count: number; | ||
increment: () => void; | ||
decrement: () => void; | ||
}; | ||
|
||
export const useCounterStore = create<CounterStore>((set) => ({ | ||
count: 0, | ||
increment: () => set((state) => ({ count: state.count + 1 })), | ||
decrement: () => set((state) => ({ count: state.count - 1 })), | ||
})); |
16 changes: 16 additions & 0 deletions
16
17. Zustand/4. Access State In Other Components/src/app/Index.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { View, Text, Button } from "react-native"; | ||
import { useCounterStore } from "../store/store"; | ||
import ChangeUI from "../components/ChangeUI"; | ||
|
||
const Index = () => { | ||
const count = useCounterStore((state) => state.count); | ||
|
||
return ( | ||
<View> | ||
<Text style={{ fontSize: 24 }}>Count: {count}</Text> | ||
<ChangeUI /> | ||
</View> | ||
); | ||
}; | ||
|
||
export default Index; |
15 changes: 15 additions & 0 deletions
15
17. Zustand/4. Access State In Other Components/src/components/ChangeUI.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { View, Text, Button } from "react-native"; | ||
import { useCounterStore } from "../store/store"; | ||
|
||
const ChangeUI = () => { | ||
const increment = useCounterStore((state) => state.increment); | ||
const decrement = useCounterStore((state) => state.decrement); | ||
|
||
return ( | ||
<View> | ||
<Button onPress={increment} title={"Increment"} /> | ||
<Button onPress={decrement} title={"Decrement"} /> | ||
</View> | ||
); | ||
}; | ||
export default ChangeUI; |
13 changes: 13 additions & 0 deletions
13
17. Zustand/4. Access State In Other Components/src/store/store.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { create } from "zustand"; | ||
|
||
type CounterStore = { | ||
count: number; | ||
increment: () => void; | ||
decrement: () => void; | ||
}; | ||
|
||
export const useCounterStore = create<CounterStore>((set) => ({ | ||
count: 0, | ||
increment: () => set((state) => ({ count: state.count + 1 })), | ||
decrement: () => set((state) => ({ count: state.count - 1 })), | ||
})); |