Skip to content

Commit 2207e4a

Browse files
committed
feat: add exemple zustand
1 parent 12ad7b1 commit 2207e4a

File tree

10 files changed

+6764
-121
lines changed

10 files changed

+6764
-121
lines changed

App.tsx

-118
This file was deleted.

__tests__/App.test.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
import 'react-native';
66
import React from 'react';
7-
import App from '../App';
7+
import App from '../src/App';
88

99
// Note: import explicitly to use the types shipped with jest.
1010
import {it} from '@jest/globals';

index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*/
44

55
import {AppRegistry} from 'react-native';
6-
import App from './App';
6+
import App from './src/App';
77
import {name as appName} from './app.json';
88

99
AppRegistry.registerComponent(appName, () => App);

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
},
1212
"dependencies": {
1313
"react": "18.2.0",
14-
"react-native": "0.73.2"
14+
"react-native": "0.73.2",
15+
"zustand": "^4.5.0"
1516
},
1617
"devDependencies": {
1718
"@babel/core": "^7.20.0",

src/App.tsx

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import React from 'react';
2+
import {SafeAreaView, ScrollView, Text, View} from 'react-native';
3+
4+
import {User} from './components/User/User';
5+
import {Product} from './components/Products/Product';
6+
7+
function App(): React.JSX.Element {
8+
console.log('Render App----');
9+
return (
10+
<SafeAreaView>
11+
<ScrollView contentInsetAdjustmentBehavior="automatic">
12+
<View
13+
style={{
14+
alignItems: 'center',
15+
}}>
16+
<Text style={{marginVertical: 50}}>Zustand</Text>
17+
<User />
18+
<Product />
19+
</View>
20+
</ScrollView>
21+
</SafeAreaView>
22+
);
23+
}
24+
25+
export default App;

src/components/Products/Product.tsx

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import React from 'react';
2+
3+
import {Button, Text, View} from 'react-native';
4+
import {useProductsStore} from '../../store/products/products';
5+
6+
export const Product = () => {
7+
const {products, addProduct, removeProduct} = useProductsStore();
8+
9+
const product = {
10+
id: Math.floor(Math.random() * 100),
11+
price: 10,
12+
name: `Product ${Math.floor(Math.random() * 100)}`,
13+
};
14+
15+
console.log('Render Product----');
16+
17+
return (
18+
<View style={{marginTop: 50}}>
19+
<Text>Product</Text>
20+
21+
<Button title="Add Product" onPress={() => addProduct(product)} />
22+
{products.length > 0 && (
23+
<Button
24+
title="Remove Product"
25+
onPress={() => removeProduct(products[0].id)}
26+
/>
27+
)}
28+
29+
{products.map((product, index) => (
30+
<View key={index} style={{flexDirection: 'row'}}>
31+
<Text>{product.id}</Text>
32+
<Text>{product.name}</Text>
33+
<Text>{product.price}</Text>
34+
</View>
35+
))}
36+
</View>
37+
);
38+
};

src/components/User/User.tsx

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import React from 'react';
2+
import {useUsersStore} from '../../store/users/users';
3+
import {Button, Text, View} from 'react-native';
4+
5+
export const User = () => {
6+
const {users, addUser, removeUser} = useUsersStore();
7+
8+
console.log('Render User----');
9+
10+
return (
11+
<View>
12+
<Text>Usuários</Text>
13+
<Button
14+
title="Add Usuário"
15+
onPress={() =>
16+
addUser({
17+
id: Math.floor(Math.random() * 100),
18+
name: `Usuário ${Math.floor(Math.random() * 100)}`,
19+
})
20+
}
21+
/>
22+
23+
{users.length > 0 && (
24+
<Button
25+
title="Remove Usuário"
26+
onPress={() => removeUser(users[0].id)}
27+
/>
28+
)}
29+
30+
{users.map((user, index) => (
31+
<Text key={index}>{user.name}</Text>
32+
))}
33+
</View>
34+
);
35+
};

src/store/products/products.ts

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import {create} from 'zustand';
2+
3+
type Product = {
4+
id: number;
5+
name: string;
6+
price: number;
7+
};
8+
9+
type ProductState = {
10+
products: Product[];
11+
addProduct: (product: Product) => void;
12+
removeProduct: (id: number) => void;
13+
};
14+
15+
export const useProductsStore = create<ProductState>(set => ({
16+
products: [],
17+
addProduct: product =>
18+
set(state => ({products: [...state.products, product]})),
19+
removeProduct: id =>
20+
set(state => ({
21+
products: state.products.filter(product => product.id !== id),
22+
})),
23+
}));

src/store/users/users.ts

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import {create} from 'zustand';
2+
3+
type User = {
4+
id: number;
5+
name: string;
6+
};
7+
8+
type UserState = {
9+
users: User[];
10+
addUser: (user: User) => void;
11+
removeUser: (id: number) => void;
12+
};
13+
14+
export const useUsersStore = create<UserState>(set => ({
15+
users: [],
16+
addUser: user => set(state => ({users: [...state.users, user]})),
17+
removeUser: id =>
18+
set(state => ({users: state.users.filter(user => user.id !== id)})),
19+
}));

0 commit comments

Comments
 (0)