Skip to content

Commit

Permalink
fix: useCollection useless re-renders (+ missing method in type) (#214)
Browse files Browse the repository at this point in the history
  • Loading branch information
HugoPerard authored Jan 16, 2024
1 parent 02c4ea4 commit 49af601
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 10 deletions.
6 changes: 1 addition & 5 deletions packages/formiz-core/src/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -703,7 +703,7 @@ export const createStore = <Values extends object = DefaultFormValues>(
get().collections.set(
fieldName.toString(),
typeof keys === "function"
? keys(get().actions.getCollectionKeys(fieldName) ?? [])
? keys(get().collections.get(fieldName) ?? [])
: keys
);
return {
Expand All @@ -712,10 +712,6 @@ export const createStore = <Values extends object = DefaultFormValues>(
});
},

getCollectionKeys: (fieldName) => {
return get().collections.get(fieldName.toString());
},

setCollectionValues: (fieldName) => (values, options) => {
set((state) => {
get().actions.setValues(
Expand Down
1 change: 0 additions & 1 deletion packages/formiz-core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,6 @@ export interface Store<Values extends object = DefaultFormValues> {
): (
keys: CollectionKey[] | ((oldKeys: CollectionKey[]) => CollectionKey[])
) => void;
getCollectionKeys(fieldName: string): CollectionKey[] | undefined;
setCollectionValues(
fieldName: string
): (
Expand Down
11 changes: 7 additions & 4 deletions packages/formiz-core/src/useCollection.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { useCallback, useEffect, useRef, useMemo } from "react";
import lodashGet from "lodash/get";
import { StoreApi, UseBoundStore } from "zustand";
import { Store } from "./types";
import { CollectionKey, Store } from "./types";
import { useFormStore } from "./Formiz";
import { deepEqual } from "fast-equals";

Expand All @@ -13,7 +13,7 @@ export interface UseCollectionOptions {
}

export type UseCollectionValues<Data = unknown> = {
keys: string[];
keys: CollectionKey[];
insertMultiple(
index: number,
data?: Partial<Data>[],
Expand All @@ -38,6 +38,9 @@ export type UseCollectionValues<Data = unknown> = {
values: unknown[],
options?: Parameters<Store["actions"]["setValues"]>[1]
): void;
setKeys(
keys: CollectionKey[] | ((oldKeys: CollectionKey[]) => CollectionKey[])
): void;
length: number;
};

Expand Down Expand Up @@ -87,14 +90,14 @@ export const useCollection = <Data = unknown>(
return {
isReady: state.ready,
keys: state.ready
? state.actions.getCollectionKeys(name) ??
? state.collections.get(name) ??
initialValuesArray.map((_, index) => index.toString())
: [],
hasInitialValues: Array.isArray(initialValues)
? !!initialValues?.length
: false,
};
});
}, deepEqual);

const collectionActions = useMemo(
() => ({
Expand Down

1 comment on commit 49af601

@vercel
Copy link

@vercel vercel bot commented on 49af601 Jan 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.