Skip to content

Commit 6643d02

Browse files
committed
fix: bug fix
1 parent 4e3aa6b commit 6643d02

File tree

2 files changed

+32
-7
lines changed

2 files changed

+32
-7
lines changed

packages/jotai-x/src/createAtomStore.spec.tsx

+29-2
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,14 @@ describe('createAtomStore', () => {
2929
arr: INITIAL_ARR,
3030
};
3131

32-
const { useMyTestStoreStore, MyTestStoreProvider, useMyTestStoreValue } =
33-
createAtomStore(initialTestStoreValue, { name: 'myTestStore' as const });
32+
const {
33+
myTestStoreStore,
34+
useMyTestStoreStore,
35+
MyTestStoreProvider,
36+
useMyTestStoreValue,
37+
} = createAtomStore(initialTestStoreValue, {
38+
name: 'myTestStore' as const,
39+
});
3440

3541
let numRenderCount = 0;
3642
const NumRenderer = () => {
@@ -121,6 +127,21 @@ describe('createAtomStore', () => {
121127
);
122128
};
123129

130+
let arrNumRenderWithDepsAndAtomCount = 0;
131+
const ArrNumRendererWithDepsAndAtom = () => {
132+
arrNumRenderWithDepsAndAtomCount += 1;
133+
const store = useMyTestStoreStore();
134+
const numAtom = myTestStoreStore.atom.num;
135+
const num = store.useAtomValue(numAtom);
136+
const arrAtom = myTestStoreStore.atom.arr;
137+
const arrNum = store.useAtomValue(arrAtom, (v) => v[num], [num]);
138+
return (
139+
<div>
140+
<div>arrNumWithDepsAndAtom: {arrNum}</div>
141+
</div>
142+
);
143+
};
144+
124145
const BadSelectorRenderer = () => {
125146
const arr0 = useMyTestStoreStore().useArrValue((v) => v[0]);
126147
return <div>{arr0}</div>;
@@ -177,6 +198,7 @@ describe('createAtomStore', () => {
177198
<ArrNumRenderer />
178199
<ArrNumRendererWithOneHook />
179200
<ArrNumRendererWithDeps />
201+
<ArrNumRendererWithDepsAndAtom />
180202
<Buttons />
181203
</MyTestStoreProvider>
182204
);
@@ -190,6 +212,7 @@ describe('createAtomStore', () => {
190212
expect(arrNumRenderCount).toBe(2);
191213
expect(arrNumRenderCountWithOneHook).toBe(2);
192214
expect(arrNumRenderWithDepsCount).toBe(2);
215+
expect(arrNumRenderWithDepsAndAtomCount).toBe(2);
193216
expect(getByText('arrNum: alice')).toBeInTheDocument();
194217
expect(getByText('arrNumWithDeps: alice')).toBeInTheDocument();
195218

@@ -202,6 +225,7 @@ describe('createAtomStore', () => {
202225
expect(arrNumRenderCount).toBe(5);
203226
expect(arrNumRenderCountWithOneHook).toBe(5);
204227
expect(arrNumRenderWithDepsCount).toBe(5);
228+
expect(arrNumRenderWithDepsAndAtomCount).toBe(5);
205229
expect(getByText('arrNum: bob')).toBeInTheDocument();
206230
expect(getByText('arrNumWithDeps: bob')).toBeInTheDocument();
207231

@@ -214,6 +238,7 @@ describe('createAtomStore', () => {
214238
expect(arrNumRenderCount).toBe(5);
215239
expect(arrNumRenderCountWithOneHook).toBe(5);
216240
expect(arrNumRenderWithDepsCount).toBe(5);
241+
expect(arrNumRenderWithDepsAndAtomCount).toBe(5);
217242
expect(getByText('arrNum: bob')).toBeInTheDocument();
218243
expect(getByText('arrNumWithDeps: bob')).toBeInTheDocument();
219244

@@ -226,6 +251,7 @@ describe('createAtomStore', () => {
226251
expect(arrNumRenderCount).toBe(5);
227252
expect(arrNumRenderCountWithOneHook).toBe(5);
228253
expect(arrNumRenderWithDepsCount).toBe(5);
254+
expect(arrNumRenderWithDepsAndAtomCount).toBe(5);
229255
expect(getByText('arrNum: bob')).toBeInTheDocument();
230256
expect(getByText('arrNumWithDeps: bob')).toBeInTheDocument();
231257

@@ -238,6 +264,7 @@ describe('createAtomStore', () => {
238264
expect(arrNumRenderCount).toBe(8);
239265
expect(arrNumRenderCountWithOneHook).toBe(8);
240266
expect(arrNumRenderWithDepsCount).toBe(8);
267+
expect(arrNumRenderWithDepsAndAtomCount).toBe(8);
241268
expect(getByText('arrNum: ava')).toBeInTheDocument();
242269
expect(getByText('arrNumWithDeps: ava')).toBeInTheDocument();
243270
});

packages/jotai-x/src/createAtomStore.ts

+3-5
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ import React from 'react';
22
import { getDefaultStore, useAtom, useAtomValue, useSetAtom } from 'jotai';
33
import { selectAtom, useHydrateAtoms } from 'jotai/utils';
44

5-
6-
75
import { atomWithFn } from './atomWithFn';
86
import { createAtomProvider, useAtomStore } from './createAtomProvider';
97

@@ -900,8 +898,8 @@ Please wrap them with useCallback or configure the deps array correctly.`
900898
store,
901899
options,
902900
selector as any,
903-
equalityFn as any,
904-
deps
901+
equalityFn ?? deps,
902+
equalityFn && deps
905903
);
906904
};
907905

@@ -1010,4 +1008,4 @@ export function useStoreAtomState<T, E, V, A extends unknown[], R>(
10101008
atom: WritableAtom<V, A, R>
10111009
) {
10121010
return store.useAtomState(atom);
1013-
}
1011+
}

0 commit comments

Comments
 (0)