Skip to content

Commit

Permalink
Memoize context value in FormProvider
Browse files Browse the repository at this point in the history
  • Loading branch information
vjee committed Feb 11, 2024
1 parent c506195 commit 981a1e1
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/tricky-birds-switch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@formulier/react": patch
---

Memoize context value in FormProvider
3 changes: 2 additions & 1 deletion packages/react/src/form.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ import type {FormContext, FormProviderProps, Selector} from './types.js'
const formContext = React.createContext<FormContext<Values, Primitives> | undefined>(undefined)

function FormProvider<V extends Values, P extends Primitives = Primitives>({form, children}: FormProviderProps<V, P>) {
return React.createElement(formContext.Provider, {value: {form: form as Formulier<Values>}}, children)
const value = React.useMemo(() => ({form: form as Formulier<Values>}), [form])
return React.createElement(formContext.Provider, {value}, children)
}

function useFormInstance<V extends Values, P extends Primitives = Primitives>() {
Expand Down

0 comments on commit 981a1e1

Please sign in to comment.