Skip to content

Commit 43ddb14

Browse files
authored
Update states object on each render (#28)
1 parent c098afc commit 43ddb14

File tree

2 files changed

+22
-26
lines changed

2 files changed

+22
-26
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@swan-io/use-form",
3-
"version": "2.0.0-rc.1",
3+
"version": "2.0.0-rc.2",
44
"license": "MIT",
55
"description": "A simple, fast and opinionated form library for React & React Native focusing on UX.",
66
"author": "Mathieu Acthernoene <[email protected]>",

src/useForm.ts

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import {
33
MutableRefObject,
44
SetStateAction,
55
useEffect,
6-
useLayoutEffect,
76
useMemo,
87
useReducer,
98
useRef,
@@ -21,9 +20,6 @@ import {
2120
Validity,
2221
} from "./types";
2322

24-
// For server-side rendering / react-native
25-
const useIsoLayoutEffect = typeof window === "undefined" ? useEffect : useLayoutEffect;
26-
2723
export const useForm = <Values extends Required<Values>, ErrorMessage = string>(
2824
config: FormConfig<Values, ErrorMessage>,
2925
): Form<Values, ErrorMessage> => {
@@ -32,12 +28,10 @@ export const useForm = <Values extends Required<Values>, ErrorMessage = string>(
3228

3329
const [, forceUpdate] = useReducer(() => [], []);
3430
const mounted = useRef(false);
35-
const arg = useRef(config);
3631
const formStatus = useRef<FormStatus>("untouched");
3732

38-
useIsoLayoutEffect(() => {
39-
arg.current = config;
40-
});
33+
const arg = useRef(config);
34+
arg.current = config;
4135

4236
useEffect(() => {
4337
mounted.current = true;
@@ -370,25 +364,27 @@ export const useForm = <Values extends Required<Values>, ErrorMessage = string>(
370364
};
371365
}, []);
372366

373-
// Lazy initialization
374-
if (!fields.current) {
375-
fields.current = {} as (typeof fields)["current"];
376-
377-
for (const name in arg.current) {
378-
if (Object.prototype.hasOwnProperty.call(arg.current, name)) {
379-
fields.current[name] = {
380-
callbacks: new Set(),
381-
ref: { current: null },
382-
mounted: false,
383-
state: {
384-
value: arg.current[name].initialValue,
385-
talkative: false,
386-
validity: { tag: "unknown" },
387-
},
388-
};
389-
}
367+
const tmp = {} as (typeof fields)["current"];
368+
369+
for (const name in arg.current) {
370+
if (Object.prototype.hasOwnProperty.call(arg.current, name)) {
371+
tmp[name] = fields.current?.[name] ?? {
372+
callbacks: new Set(),
373+
ref: { current: null },
374+
mounted: false,
375+
state: {
376+
value: arg.current[name].initialValue,
377+
talkative: false,
378+
validity: { tag: "unknown" },
379+
},
380+
};
390381
}
382+
}
391383

384+
fields.current = tmp;
385+
386+
// Lazy initialization
387+
if (!field.current) {
392388
const Field: Contract["Field"] = ({ name, children }) => {
393389
const { subscribe, getSnapshot } = useMemo(
394390
() => ({

0 commit comments

Comments
 (0)