Skip to content

Commit 62d6637

Browse files
authored
Added the use of useShallow instead of shallow to construct a single object with multiple state picks (#36)
* Refactored zustand-pick AST to use `useShallow` instead of `shallow` * Updated tests for zustand-pick as per useShallow use * Updated zustand-pick documentation to reflect the use of `useShallow` instead of `shallow`.
1 parent 6bc96b8 commit 62d6637

12 files changed

Lines changed: 48 additions & 59 deletions

File tree

docs/zustand-pick.md

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,13 @@ When we wanted to pick some properties from a zustand store, this is what we
66
need to do:
77

88
```js
9-
import { shallow } from "zustand/shallow";
9+
import { useShallow } from "zustand/shallow";
1010

1111
const { order, customer } = useGlobalStore(
12-
store => ({
12+
useShallow(store => ({
1313
order: store[sessionId]?.globals.order,
1414
customer: store[sessionId]?.globals.customer,
15-
}),
16-
shallow
15+
}))
1716
);
1817
```
1918
@@ -30,14 +29,14 @@ const { order, customer } = useGlobalStore.pick([sessionId, "globals"]);
3029
```
3130
3231
You might also notice that we don't have the
33-
`import { shallow } from "zustand/shallow"` statement in this piece of code. We
34-
don't need that. The plugin will automatically add it for us at the time of
32+
`import { useShallow } from "zustand/shallow"` statement in this piece of code.
33+
We don't need that. The plugin will automatically add it for us at the time of
3534
transpiling.
3635
3736
The plugin detects that `order` and `customer` properties are to be fetched from
3837
the `store[sessionId].globals` property from `useGlobalStore` and it generates
3938
code for it. Note that the plugin will automatically add
40-
`import { shallow } from "zustand/shallow"` for us. We don't need to add it
39+
`import { useShallow } from "zustand/shallow"` for us. We don't need to add it
4140
ourselves.
4241
4342
## Allowed syntaxes
@@ -170,13 +169,12 @@ const { order, customer } = useGlobalStore.pick([sessionId, "globals"]);
170169
Will be transformed to:
171170
172171
```js
173-
import { shallow } from "zustand/shallow";
172+
import { useShallow } from "zustand/shallow";
174173
const { order, customer } = useGlobalStore(
175-
store => ({
174+
useShallow(store => ({
176175
order: store[sessionId]?.["globals"]?.["order"],
177176
customer: store[sessionId]?.["globals"]?.["customer"],
178-
}),
179-
shallow
177+
}))
180178
);
181179
```
182180

src/plugins/zustand-pick/index.js

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -62,21 +62,22 @@ module.exports = function ({ types }) {
6262
declarator.init = types.callExpression(
6363
types.identifier(declarator.init.callee.object.name),
6464
[
65-
types.arrowFunctionExpression(
66-
[types.identifier("store")],
67-
types.objectExpression(
68-
declarator.id.properties.map(toObjectProperty)
69-
)
70-
),
71-
types.identifier("shallow"),
65+
types.callExpression(types.identifier("useShallow"), [
66+
types.arrowFunctionExpression(
67+
[types.identifier("store")],
68+
types.objectExpression(
69+
declarator.id.properties.map(toObjectProperty)
70+
)
71+
),
72+
]),
7273
]
7374
);
7475

7576
addNamedImport({
7677
path: astPath,
7778
types,
7879
source: "zustand/shallow",
79-
importName: "shallow",
80+
importName: "useShallow",
8081
});
8182
},
8283
CallExpression(astPath) {
Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
import { shallow } from "zustand/shallow";
1+
import { useShallow } from "zustand/shallow";
22
const { order, customer } = useGlobalStore(
3-
store => ({
3+
useShallow(store => ({
44
order: store["state"]?.[sessionId]?.["current"]?.[data]?.["order"],
55
customer: store["state"]?.[sessionId]?.["current"]?.[data]?.["customer"],
6-
}),
7-
shallow
6+
}))
87
);
Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
import { shallow } from "zustand/shallow";
1+
import { useShallow } from "zustand/shallow";
22
const { orderId = "#1001", customer = {} } = useGlobalStore(
3-
store => ({
3+
useShallow(store => ({
44
orderId: store["orderId"],
55
customer: store["customer"],
6-
}),
7-
shallow
6+
}))
87
);
Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
1-
import { shallow } from "zustand/shallow";
1+
import { useShallow } from "zustand/shallow";
22
const { [`customer ${id}`]: customer } = useGlobalStore(
3-
store => ({
3+
useShallow(store => ({
44
[`customer ${id}`]: store[sessionId]?.[`customer ${id}`],
5-
}),
6-
shallow
5+
}))
76
);
87
const { [orderId]: order } = useGlobalStore(
9-
store => ({
8+
useShallow(store => ({
109
[orderId]: store[sessionId]?.["orders"]?.[orderId],
11-
}),
12-
shallow
10+
}))
1311
);
Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
import { shallow } from "zustand/shallow";
1+
import { useShallow } from "zustand/shallow";
22
const { customer } = useGlobalStore(
3-
store => ({
3+
useShallow(store => ({
44
customer: store[sessionId]?.["customer"],
5-
}),
6-
shallow
5+
}))
76
);
Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
import { shallow } from "zustand/shallow";
1+
import { useShallow } from "zustand/shallow";
22
const { order, customer } = useGlobalStore(
3-
store => ({
3+
useShallow(store => ({
44
order: store["order"],
55
customer: store["customer"],
6-
}),
7-
shallow
6+
}))
87
);
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
import { shallow } from "zustand/shallow";
1+
import { useShallow } from "zustand/shallow";
22
const { order, customer } = useGlobalStore.pick();
Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
import { shallow } from "zustand/shallow";
1+
import { useShallow } from "zustand/shallow";
22
const { order, customer } = useGlobalStore(
3-
store => ({
3+
useShallow(store => ({
44
order: store["order"],
55
customer: store["customer"],
6-
}),
7-
shallow
6+
}))
87
);
Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
import { shallow } from "zustand/shallow";
1+
import { useShallow } from "zustand/shallow";
22
const { order, customer, address } = useGlobalStore(
3-
store => ({
3+
useShallow(store => ({
44
order: store["state"]?.["order"],
55
customer: store["state"]?.["customer"],
66
address: store["state"]?.["address"],
7-
}),
8-
shallow
7+
}))
98
);

0 commit comments

Comments
 (0)