Skip to content

Commit 3d28935

Browse files
MinJieLiuzombieJ
authored andcommitted
fix: form.resetFields cause field component mount twice (#65)
* fix: form.resetFields 会导致 field 子组件 mount 两次 * review
1 parent 18f88bf commit 3d28935

File tree

2 files changed

+21
-19
lines changed

2 files changed

+21
-19
lines changed

src/Field.tsx

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ export interface FieldProps {
6767
}
6868

6969
export interface FieldState {
70-
reset: boolean;
70+
resetCount: number;
7171
}
7272

7373
// We use Class instead of Hooks here since it will cost much code by using Hooks.
@@ -82,7 +82,7 @@ class Field extends React.Component<FieldProps, FieldState>
8282
};
8383

8484
public state = {
85-
reset: false,
85+
resetCount: 0,
8686
};
8787

8888
private cancelRegisterFunc: () => void | null = null;
@@ -151,17 +151,11 @@ class Field extends React.Component<FieldProps, FieldState>
151151
if (this.destroy) return;
152152

153153
/**
154-
* We update `reset` state twice to clean up current node.
155-
* Which helps to reset value without define the type.
154+
* Clean up current node.
156155
*/
157-
this.setState(
158-
{
159-
reset: true,
160-
},
161-
() => {
162-
this.setState({ reset: false });
163-
},
164-
);
156+
this.setState(({ resetCount }) => ({
157+
resetCount: resetCount + 1,
158+
}));
165159
};
166160

167161
// ========================= Field Entity Interfaces =========================
@@ -453,7 +447,7 @@ class Field extends React.Component<FieldProps, FieldState>
453447
};
454448

455449
public render() {
456-
const { reset } = this.state;
450+
const { resetCount } = this.state;
457451
const { children } = this.props;
458452

459453
const { child, isFunction } = this.getOnlyChild(children);
@@ -472,12 +466,7 @@ class Field extends React.Component<FieldProps, FieldState>
472466
returnChildNode = child;
473467
}
474468

475-
// Force render a new component to reset all the data
476-
if (reset) {
477-
return React.createElement(() => <>{returnChildNode}</>);
478-
}
479-
480-
return returnChildNode;
469+
return <React.Fragment key={resetCount}>{returnChildNode}</React.Fragment>;
481470
}
482471
}
483472

tests/index.test.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,15 @@ describe('Form.Basic', () => {
246246

247247
it('update and reset should use new initialValues', () => {
248248
let form;
249+
let mountCount = 0;
250+
251+
const TestInput = props => {
252+
React.useEffect(() => {
253+
mountCount += 1;
254+
}, []);
255+
256+
return <Input {...props} />;
257+
};
249258

250259
const Test = ({ initialValues }) => (
251260
<Form
@@ -257,6 +266,9 @@ describe('Form.Basic', () => {
257266
<Field name="username">
258267
<Input />
259268
</Field>
269+
<Field name="email">
270+
<TestInput />
271+
</Field>
260272
</Form>
261273
);
262274

@@ -285,6 +297,7 @@ describe('Form.Basic', () => {
285297
// Should change it
286298
form.resetFields();
287299
wrapper.update();
300+
expect(mountCount).toEqual(1);
288301
expect(form.getFieldsValue()).toEqual({
289302
username: 'Light',
290303
});

0 commit comments

Comments
 (0)