Skip to content

Commit 2fa1c82

Browse files
authored
Fixed bug with postmount updates (#197)
1 parent e259835 commit 2fa1c82

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

src/ReactFinalForm.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ export default class ReactFinalForm extends React.Component<Props, State> {
102102
if (this.mounted) {
103103
this.setState({ state })
104104
}
105+
this.mounted = true
105106
}
106107

107108
handleSubmit = (event?: SyntheticEvent<HTMLFormElement>) => {
@@ -125,7 +126,6 @@ export default class ReactFinalForm extends React.Component<Props, State> {
125126
)
126127
this.form.resumeValidation()
127128
}
128-
this.mounted = true
129129
}
130130

131131
componentWillReceiveProps(nextProps: Props) {

src/ReactFinalForm.test.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,28 @@ describe('ReactFinalForm', () => {
390390
expect(unsubscribe).toHaveBeenCalled()
391391
})
392392

393+
it('should show form as invalid on first load if field-level validation errors are present', () => {
394+
// Debugging https://github.com/final-form/react-final-form/issues/196
395+
const render = jest.fn()
396+
TestUtils.renderIntoDocument(
397+
<Form onSubmit={onSubmitMock}>
398+
{({ invalid }) => {
399+
render(invalid)
400+
return (
401+
<Field
402+
name="foo"
403+
component="input"
404+
validate={value => (value ? undefined : 'Required')}
405+
/>
406+
)
407+
}}
408+
</Form>
409+
)
410+
expect(render).toHaveBeenCalledTimes(2)
411+
expect(render.mock.calls[0][0]).toBe(false)
412+
expect(render.mock.calls[1][0]).toBe(true)
413+
})
414+
393415
it('should work with server-side rendering', () => {
394416
const spy = jest.spyOn(global.console, 'error')
395417
ReactDOMServer.renderToString(

0 commit comments

Comments
 (0)