Skip to content

Commit

Permalink
馃悶 fix #3508 state update with watch (#3511)
Browse files Browse the repository at this point in the history
  • Loading branch information
bluebill1049 committed Nov 24, 2020
1 parent 6dcff1f commit 83c0f73
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 81 deletions.
10 changes: 5 additions & 5 deletions package.json
Expand Up @@ -71,7 +71,7 @@
"author": "<bluebill1049@hotmail.com>",
"license": "MIT",
"devDependencies": {
"@babel/core": "^7.12.7",
"@babel/core": "^7.12.8",
"@babel/plugin-transform-runtime": "^7.10.4",
"@changesets/changelog-github": "^0.2.6",
"@changesets/cli": "^2.11.2",
Expand All @@ -86,16 +86,16 @@
"@types/jest": "^26.0.15",
"@types/react": "^17.0.0",
"@types/react-dom": "^17.0.0",
"@types/react-native": "^0.63.36",
"@types/react-native": "^0.63.37",
"@types/react-test-renderer": "^17.0.0",
"@typescript-eslint/eslint-plugin": "^4.8.1",
"@typescript-eslint/parser": "^4.8.1",
"@typescript-eslint/eslint-plugin": "^4.8.2",
"@typescript-eslint/parser": "^4.8.2",
"babel-jest": "^26.6.3",
"bundlesize": "^0.18.0",
"copyfiles": "^2.3.0",
"core-js": "^3.7.0",
"coveralls": "^3.1.0",
"cypress": "5.6.0",
"cypress": "6.0.0",
"cypress-parallel": "^0.1.8",
"eslint": "^7.14.0",
"eslint-config-prettier": "^6.15.0",
Expand Down
2 changes: 2 additions & 0 deletions src/useFieldArray.test.tsx
Expand Up @@ -784,6 +784,7 @@ describe('useFieldArray', () => {
control,
formState: tempFormState,
setValue: tempSetValue,
watch,
} = useForm({
defaultValues: {
test: [
Expand All @@ -794,6 +795,7 @@ describe('useFieldArray', () => {
},
});
const { fields } = useFieldArray({ name: 'test', control });
watch();

setValue = tempSetValue;
formState = tempFormState;
Expand Down
3 changes: 0 additions & 3 deletions src/useFieldArray.ts
Expand Up @@ -285,10 +285,7 @@ export const useFieldArray = <
}

updateFormState({
errors: formStateRef.current.errors,
dirtyFields: formStateRef.current.dirtyFields,
isDirty: isFormDirty(name, omitKey(updatedFormValues)),
touched: formStateRef.current.touched,
});
};

Expand Down
28 changes: 10 additions & 18 deletions src/useForm.ts
Expand Up @@ -169,12 +169,15 @@ export function useForm<
: shallowFieldsStateRef.current;

const updateFormState = React.useCallback(
(state: Partial<FormState<TFieldValues>> = {}) =>
!isUnMount.current &&
setFormState({
...formStateRef.current,
...state,
}),
(state: Partial<FormState<TFieldValues>> = {}) => {
if (!isUnMount.current) {
formStateRef.current = {
...formStateRef.current,
...state,
};
setFormState(formStateRef.current);
}
},
[],
);

Expand Down Expand Up @@ -223,7 +226,6 @@ export function useForm<
) {
updateFormState({
...state,
errors: formStateRef.current.errors,
...(resolverRef.current ? { isValid: !!isValid } : {}),
});
}
Expand Down Expand Up @@ -336,12 +338,8 @@ export function useForm<
isDirtyFieldExist !== get(formStateRef.current.dirtyFields, name));

if (isChanged && shouldRender) {
formStateRef.current = {
...formStateRef.current,
...state,
};
updateFormState({
...formStateRef.current,
...state,
});
}

Expand Down Expand Up @@ -409,7 +407,6 @@ export function useForm<

updateFormState({
isValid: isEmptyObject(errors),
errors: formStateRef.current.errors,
});

return isInputsValid;
Expand Down Expand Up @@ -517,7 +514,6 @@ export function useForm<
{ ...getValues(), [name]: value },
defaultValuesRef.current,
),
dirtyFields: formStateRef.current.dirtyFields,
});
}
}
Expand Down Expand Up @@ -774,9 +770,7 @@ export function useForm<
set(formStateRef.current.dirtyFields, field.ref.name, true);

updateFormState({
errors: formStateRef.current.errors,
isDirty: isFormDirty(),
dirtyFields: formStateRef.current.dirtyFields,
});

readFormStateRef.current.isValid &&
Expand Down Expand Up @@ -814,7 +808,6 @@ export function useForm<

updateFormState({
isValid: false,
errors: formStateRef.current.errors,
});

error.shouldFocus && ref && ref.focus && ref.focus();
Expand Down Expand Up @@ -1189,7 +1182,6 @@ export function useForm<
isSubmitted: true,
isSubmitting: false,
isSubmitSuccessful: isEmptyObject(formStateRef.current.errors),
errors: formStateRef.current.errors,
submitCount: formStateRef.current.submitCount + 1,
});
}
Expand Down
125 changes: 70 additions & 55 deletions yarn.lock
Expand Up @@ -53,18 +53,18 @@
semver "^5.4.1"
source-map "^0.5.0"

"@babel/core@^7.12.7":
version "7.12.7"
resolved "https://registry.yarnpkg.com/@babel/core/-/core-7.12.7.tgz#bf55363c08c8352a37691f7216ec30090bf7e3bf"
integrity sha512-tRKx9B53kJe8NCGGIxEQb2Bkr0riUIEuN7Sc1fxhs5H8lKlCWUvQCSNMVIB0Meva7hcbCRJ76de15KoLltdoqw==
"@babel/core@^7.12.8":
version "7.12.8"
resolved "https://registry.yarnpkg.com/@babel/core/-/core-7.12.8.tgz#8ad76c1a7d2a6a3beecc4395fa4f7b4cb88390e6"
integrity sha512-ra28JXL+5z73r1IC/t+FT1ApXU5LsulFDnTDntNfLQaScJUJmcHL5Qxm/IWanCToQk3bPWQo5bflbplU5r15pg==
dependencies:
"@babel/code-frame" "^7.10.4"
"@babel/generator" "^7.12.5"
"@babel/helper-module-transforms" "^7.12.1"
"@babel/helpers" "^7.12.5"
"@babel/parser" "^7.12.7"
"@babel/template" "^7.12.7"
"@babel/traverse" "^7.12.7"
"@babel/traverse" "^7.12.8"
"@babel/types" "^7.12.7"
convert-source-map "^1.7.0"
debug "^4.1.0"
Expand Down Expand Up @@ -911,7 +911,7 @@
globals "^11.1.0"
lodash "^4.17.19"

"@babel/traverse@^7.12.5", "@babel/traverse@^7.12.7":
"@babel/traverse@^7.12.5":
version "7.12.7"
resolved "https://registry.yarnpkg.com/@babel/traverse/-/traverse-7.12.7.tgz#572a722408681cef17d6b0bef69ef2e728ca69f1"
integrity sha512-nMWaqsQEeSvMNypswUDzjqQ+0rR6pqCtoQpsqGJC4/Khm9cISwPTSpai57F6/jDaOoEGz8yE/WxcO3PV6tKSmQ==
Expand All @@ -926,6 +926,21 @@
globals "^11.1.0"
lodash "^4.17.19"

"@babel/traverse@^7.12.8":
version "7.12.8"
resolved "https://registry.yarnpkg.com/@babel/traverse/-/traverse-7.12.8.tgz#c1c2983bf9ba0f4f0eaa11dff7e77fa63307b2a4"
integrity sha512-EIRQXPTwFEGRZyu6gXbjfpNORN1oZvwuzJbxcXjAgWV0iqXYDszN1Hx3FVm6YgZfu1ZQbCVAk3l+nIw95Xll9Q==
dependencies:
"@babel/code-frame" "^7.10.4"
"@babel/generator" "^7.12.5"
"@babel/helper-function-name" "^7.10.4"
"@babel/helper-split-export-declaration" "^7.11.0"
"@babel/parser" "^7.12.7"
"@babel/types" "^7.12.7"
debug "^4.1.0"
globals "^11.1.0"
lodash "^4.17.19"

"@babel/types@^7.0.0", "@babel/types@^7.10.4", "@babel/types@^7.11.0", "@babel/types@^7.11.5", "@babel/types@^7.12.1", "@babel/types@^7.3.0", "@babel/types@^7.3.3":
version "7.12.1"
resolved "https://registry.yarnpkg.com/@babel/types/-/types-7.12.1.tgz#e109d9ab99a8de735be287ee3d6a9947a190c4ae"
Expand Down Expand Up @@ -2068,10 +2083,10 @@
dependencies:
"@types/react" "*"

"@types/react-native@^0.63.36":
version "0.63.36"
resolved "https://registry.yarnpkg.com/@types/react-native/-/react-native-0.63.36.tgz#649ca8fd2b0cda9e41b961e7501e33b71246e88d"
integrity sha512-mCWEiPgi55MkXXMuDe0VsOWO3C4hhj6mO/jnTvy0sXcRD6MbAmGwXnVe5+m1FRCuEQMaJwgG1ZNahZvvyvuGUA==
"@types/react-native@^0.63.37":
version "0.63.37"
resolved "https://registry.yarnpkg.com/@types/react-native/-/react-native-0.63.37.tgz#c43df90c9d3cc082a97a49a53e989de26cb8ab45"
integrity sha512-xr9SZG7tQQBKT6840tAGaWEC65D2gjyxZtuZxz631UgeW1ofItuu9HMVhoyYqot2hRSa6Q4YC8FYkRVUpM53/w==
dependencies:
"@types/react" "*"

Expand Down Expand Up @@ -2180,74 +2195,74 @@
dependencies:
"@types/yargs-parser" "*"

"@typescript-eslint/eslint-plugin@^4.8.1":
version "4.8.1"
resolved "https://registry.yarnpkg.com/@typescript-eslint/eslint-plugin/-/eslint-plugin-4.8.1.tgz#b362abe0ee478a6c6d06c14552a6497f0b480769"
integrity sha512-d7LeQ7dbUrIv5YVFNzGgaW3IQKMmnmKFneRWagRlGYOSfLJVaRbj/FrBNOBC1a3tVO+TgNq1GbHvRtg1kwL0FQ==
"@typescript-eslint/eslint-plugin@^4.8.2":
version "4.8.2"
resolved "https://registry.yarnpkg.com/@typescript-eslint/eslint-plugin/-/eslint-plugin-4.8.2.tgz#cf9102ec800391caa574f589ffe0623cca1d9308"
integrity sha512-gQ06QLV5l1DtvYtqOyFLXD9PdcILYqlrJj2l+CGDlPtmgLUzc1GpqciJFIRvyfvgLALpnxYINFuw+n9AZhPBKQ==
dependencies:
"@typescript-eslint/experimental-utils" "4.8.1"
"@typescript-eslint/scope-manager" "4.8.1"
"@typescript-eslint/experimental-utils" "4.8.2"
"@typescript-eslint/scope-manager" "4.8.2"
debug "^4.1.1"
functional-red-black-tree "^1.0.1"
regexpp "^3.0.0"
semver "^7.3.2"
tsutils "^3.17.1"

"@typescript-eslint/experimental-utils@4.8.1":
version "4.8.1"
resolved "https://registry.yarnpkg.com/@typescript-eslint/experimental-utils/-/experimental-utils-4.8.1.tgz#27275c20fa4336df99ebcf6195f7d7aa7aa9f22d"
integrity sha512-WigyLn144R3+lGATXW4nNcDJ9JlTkG8YdBWHkDlN0lC3gUGtDi7Pe3h5GPvFKMcRz8KbZpm9FJV9NTW8CpRHpg==
"@typescript-eslint/experimental-utils@4.8.2":
version "4.8.2"
resolved "https://registry.yarnpkg.com/@typescript-eslint/experimental-utils/-/experimental-utils-4.8.2.tgz#8909a5732f19329cf5ef0c39766170476bff5e50"
integrity sha512-hpTw6o6IhBZEsQsjuw/4RWmceRyESfAiEzAEnXHKG1X7S5DXFaZ4IO1JO7CW1aQ604leQBzjZmuMI9QBCAJX8Q==
dependencies:
"@types/json-schema" "^7.0.3"
"@typescript-eslint/scope-manager" "4.8.1"
"@typescript-eslint/types" "4.8.1"
"@typescript-eslint/typescript-estree" "4.8.1"
"@typescript-eslint/scope-manager" "4.8.2"
"@typescript-eslint/types" "4.8.2"
"@typescript-eslint/typescript-estree" "4.8.2"
eslint-scope "^5.0.0"
eslint-utils "^2.0.0"

"@typescript-eslint/parser@^4.8.1":
version "4.8.1"
resolved "https://registry.yarnpkg.com/@typescript-eslint/parser/-/parser-4.8.1.tgz#4fe2fbdbb67485bafc4320b3ae91e34efe1219d1"
integrity sha512-QND8XSVetATHK9y2Ltc/XBl5Ro7Y62YuZKnPEwnNPB8E379fDsvzJ1dMJ46fg/VOmk0hXhatc+GXs5MaXuL5Uw==
"@typescript-eslint/parser@^4.8.2":
version "4.8.2"
resolved "https://registry.yarnpkg.com/@typescript-eslint/parser/-/parser-4.8.2.tgz#78dccbe5124de2b8dea2d4c363dee9f769151ca8"
integrity sha512-u0leyJqmclYr3KcXOqd2fmx6SDGBO0MUNHHAjr0JS4Crbb3C3d8dwAdlazy133PLCcPn+aOUFiHn72wcuc5wYw==
dependencies:
"@typescript-eslint/scope-manager" "4.8.1"
"@typescript-eslint/types" "4.8.1"
"@typescript-eslint/typescript-estree" "4.8.1"
"@typescript-eslint/scope-manager" "4.8.2"
"@typescript-eslint/types" "4.8.2"
"@typescript-eslint/typescript-estree" "4.8.2"
debug "^4.1.1"

"@typescript-eslint/scope-manager@4.8.1":
version "4.8.1"
resolved "https://registry.yarnpkg.com/@typescript-eslint/scope-manager/-/scope-manager-4.8.1.tgz#e343c475f8f1d15801b546cb17d7f309b768fdce"
integrity sha512-r0iUOc41KFFbZdPAdCS4K1mXivnSZqXS5D9oW+iykQsRlTbQRfuFRSW20xKDdYiaCoH+SkSLeIF484g3kWzwOQ==
"@typescript-eslint/scope-manager@4.8.2":
version "4.8.2"
resolved "https://registry.yarnpkg.com/@typescript-eslint/scope-manager/-/scope-manager-4.8.2.tgz#a18388c63ae9c17adde519384f539392f2c4f0d9"
integrity sha512-qHQ8ODi7mMin4Sq2eh/6eu03uVzsf5TX+J43xRmiq8ujng7ViQSHNPLOHGw/Wr5dFEoxq/ubKhzClIIdQy5q3g==
dependencies:
"@typescript-eslint/types" "4.8.1"
"@typescript-eslint/visitor-keys" "4.8.1"
"@typescript-eslint/types" "4.8.2"
"@typescript-eslint/visitor-keys" "4.8.2"

"@typescript-eslint/types@4.8.1":
version "4.8.1"
resolved "https://registry.yarnpkg.com/@typescript-eslint/types/-/types-4.8.1.tgz#23829c73c5fc6f4fcd5346a7780b274f72fee222"
integrity sha512-ave2a18x2Y25q5K05K/U3JQIe2Av4+TNi/2YuzyaXLAsDx6UZkz1boZ7nR/N6Wwae2PpudTZmHFXqu7faXfHmA==
"@typescript-eslint/types@4.8.2":
version "4.8.2"
resolved "https://registry.yarnpkg.com/@typescript-eslint/types/-/types-4.8.2.tgz#c862dd0e569d9478eb82d6aee662ea53f5661a36"
integrity sha512-z1/AVcVF8ju5ObaHe2fOpZYEQrwHyZ7PTOlmjd3EoFeX9sv7UekQhfrCmgUO7PruLNfSHrJGQvrW3Q7xQ8EoAw==

"@typescript-eslint/typescript-estree@4.8.1":
version "4.8.1"
resolved "https://registry.yarnpkg.com/@typescript-eslint/typescript-estree/-/typescript-estree-4.8.1.tgz#7307e3f2c9e95df7daa8dc0a34b8c43b7ec0dd32"
integrity sha512-bJ6Fn/6tW2g7WIkCWh3QRlaSU7CdUUK52shx36/J7T5oTQzANvi6raoTsbwGM11+7eBbeem8hCCKbyvAc0X3sQ==
"@typescript-eslint/typescript-estree@4.8.2":
version "4.8.2"
resolved "https://registry.yarnpkg.com/@typescript-eslint/typescript-estree/-/typescript-estree-4.8.2.tgz#eeec34707d8577600fb21661b5287226cc8b3bed"
integrity sha512-HToGNwI6fekH0dOw3XEVESUm71Onfam0AKin6f26S2FtUmO7o3cLlWgrIaT1q3vjB3wCTdww3Dx2iGq5wtUOCg==
dependencies:
"@typescript-eslint/types" "4.8.1"
"@typescript-eslint/visitor-keys" "4.8.1"
"@typescript-eslint/types" "4.8.2"
"@typescript-eslint/visitor-keys" "4.8.2"
debug "^4.1.1"
globby "^11.0.1"
is-glob "^4.0.1"
lodash "^4.17.15"
semver "^7.3.2"
tsutils "^3.17.1"

"@typescript-eslint/visitor-keys@4.8.1":
version "4.8.1"
resolved "https://registry.yarnpkg.com/@typescript-eslint/visitor-keys/-/visitor-keys-4.8.1.tgz#794f68ee292d1b2e3aa9690ebedfcb3a8c90e3c3"
integrity sha512-3nrwXFdEYALQh/zW8rFwP4QltqsanCDz4CwWMPiIZmwlk9GlvBeueEIbq05SEq4ganqM0g9nh02xXgv5XI3PeQ==
"@typescript-eslint/visitor-keys@4.8.2":
version "4.8.2"
resolved "https://registry.yarnpkg.com/@typescript-eslint/visitor-keys/-/visitor-keys-4.8.2.tgz#62cd3fbbbf65f8eccfbe6f159eb1b84a243a3f77"
integrity sha512-Vg+/SJTMZJEKKGHW7YC21QxgKJrSbxoYYd3MEUGtW7zuytHuEcksewq0DUmo4eh/CTNrVJGSdIY9AtRb6riWFw==
dependencies:
"@typescript-eslint/types" "4.8.1"
"@typescript-eslint/types" "4.8.2"
eslint-visitor-keys "^2.0.0"

abab@^2.0.3:
Expand Down Expand Up @@ -3651,10 +3666,10 @@ cypress-parallel@^0.1.8:
mocha "^8.0.1"
yargs "15.3.1"

cypress@5.6.0:
version "5.6.0"
resolved "https://registry.yarnpkg.com/cypress/-/cypress-5.6.0.tgz#6781755c3ddfd644ce3179fcd7389176c0c82280"
integrity sha512-cs5vG3E2JLldAc16+5yQxaVRLLqMVya5RlrfPWkC72S5xrlHFdw7ovxPb61s4wYweROKTyH01WQc2PFzwwVvyQ==
cypress@6.0.0:
version "6.0.0"
resolved "https://registry.yarnpkg.com/cypress/-/cypress-6.0.0.tgz#57050773c61e8fe1e5c9871cc034c616fcacded9"
integrity sha512-A/w9S15xGxX5UVeAQZacKBqaA0Uqlae9e5WMrehehAdFiLOZj08IgSVZOV8YqA9OH9Z0iBOnmsEkK3NNj43VrA==
dependencies:
"@cypress/listr-verbose-renderer" "^0.4.1"
"@cypress/request" "^2.88.5"
Expand Down

0 comments on commit 83c0f73

Please sign in to comment.