Skip to content

Commit fe2fb36

Browse files
Jaideep PannuJaideep Pannu
Jaideep Pannu
authored and
Jaideep Pannu
committed
fix(useField): Fix no re-render on initialValue change
1 parent 43952cb commit fe2fb36

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

src/useField.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,11 +112,14 @@ function useField<FormValues: FormValuesShape>(
112112

113113
React.useEffect(
114114
() =>
115-
register((state) => {
115+
register((newState) => {
116116
if (firstRender.current) {
117117
firstRender.current = false;
118+
if(state.initial != newState.initial){
119+
setState(newState);
120+
}
118121
} else {
119-
setState(state);
122+
setState(newState);
120123
}
121124
}, false),
122125
// eslint-disable-next-line react-hooks/exhaustive-deps

src/useField.test.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -455,4 +455,27 @@ describe("useField", () => {
455455
expect(spy.mock.calls[3][1]).toBe(spy.mock.calls[2][1]); // onFocus
456456
expect(spy.mock.calls[3][2]).toBe(spy.mock.calls[2][2]); // onBlur
457457
});
458+
459+
it("should listen to initial value 2", () => {
460+
const MyFieldListener = () => {
461+
const isFirstRender = React.useRef(true)
462+
const { input, meta } = useField("name");
463+
if(!isFirstRender.current){
464+
expect(meta.initial).toBe("test");
465+
// expect(input.value).toBe("test");
466+
}
467+
isFirstRender.current = false
468+
return null;
469+
};
470+
render(
471+
<Form onSubmit={onSubmitMock}>
472+
{() => (
473+
<form>
474+
<MyFieldListener />
475+
<Field name="name" component="input" data-testid="name" initialValue="test"/>
476+
</form>
477+
)}
478+
</Form>,
479+
);
480+
});
458481
});

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy