Skip to content

Commit ca7f26b

Browse files
authored
fix(react-form): retain field and form context during Hot Module Replacement (#1641)
refactor(react-form): move context creation to module scope this avoids HMR creating new context and breaking the connection by accident.
1 parent ecfd47f commit ca7f26b

File tree

1 file changed

+4
-6
lines changed

1 file changed

+4
-6
lines changed

packages/react-form/src/createFormHook.tsx

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ import type { FieldComponent } from './useField'
1818
import type { ReactFormExtendedApi } from './useForm'
1919
import type { AppFieldExtendedReactFieldGroupApi } from './useFieldGroup'
2020

21+
// We should never hit the `null` case here
22+
const fieldContext = createContext<AnyFieldApi>(null as never)
23+
const formContext = createContext<AnyFormApi>(null as never)
24+
2125
/**
2226
* TypeScript inferencing is weird.
2327
*
@@ -56,9 +60,6 @@ type UnwrapDefaultOrAny<DefaultT, T> = [DefaultT] extends [T]
5660
: T
5761

5862
export function createFormHookContexts() {
59-
// We should never hit the `null` case here
60-
const fieldContext = createContext<AnyFieldApi>(null as never)
61-
6263
function useFieldContext<TData>() {
6364
const field = useContext(fieldContext)
6465

@@ -92,9 +93,6 @@ export function createFormHookContexts() {
9293
>
9394
}
9495

95-
// We should never hit the `null` case here
96-
const formContext = createContext<AnyFormApi>(null as never)
97-
9896
function useFormContext() {
9997
const form = useContext(formContext)
10098

0 commit comments

Comments
 (0)