Skip to content

Commit 34b289b

Browse files
authored
Outlaw subscription changes (#497)
1 parent de0f627 commit 34b289b

File tree

7 files changed

+7
-55
lines changed

7 files changed

+7
-55
lines changed

src/FormSpy.test.js

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ describe('FormSpy', () => {
9797
expect(spy.mock.calls[2][0].values).toEqual({ name: 'erikras' })
9898
})
9999

100-
it('should resubscribe if subscription changes', () => {
100+
it('should NOT resubscribe if subscription changes', () => {
101101
const firstSubscription = { values: true, pristine: true }
102102
const secondSubscription = { dirty: true, submitting: true }
103103
const spy = jest.fn()
@@ -165,19 +165,8 @@ describe('FormSpy', () => {
165165

166166
fireEvent.click(getByText('Toggle'))
167167

168-
// one for new prop, and again after reregistering
169-
expect(spy).toHaveBeenCalledTimes(4)
170-
hasFormApi(spy.mock.calls[3][0])
171-
expect(spy.mock.calls[3][0].dirty).toBe(false)
172-
expect(spy.mock.calls[3][0].errors).toBeUndefined()
173-
expect(spy.mock.calls[3][0].invalid).toBeUndefined()
174-
expect(spy.mock.calls[3][0].pristine).toBeUndefined()
175-
expect(spy.mock.calls[3][0].submitFailed).toBeUndefined()
176-
expect(spy.mock.calls[3][0].submitSucceeded).toBeUndefined()
177-
expect(spy.mock.calls[3][0].submitting).toBe(false)
178-
expect(spy.mock.calls[3][0].valid).toBeUndefined()
179-
expect(spy.mock.calls[3][0].validating).toBeUndefined()
180-
expect(spy.mock.calls[3][0].values).toBeUndefined()
168+
// one for new prop, and NOT again because no reregistering since v6
169+
expect(spy).toHaveBeenCalledTimes(3)
181170
})
182171

183172
it('should hear changes', () => {

src/ReactFinalForm.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import useConstant from './useConstant'
1919
import shallowEqual from './shallowEqual'
2020
import isSyntheticEvent from './isSyntheticEvent'
2121
import type { FormRenderProps } from './types.js.flow'
22-
import flattenSubscription from './flattenSubscription'
2322
import ReactFinalFormContext from './context'
2423

2524
export const version = '5.1.0'
@@ -79,8 +78,6 @@ const ReactFinalForm = ({
7978
}
8079
)
8180

82-
// In the future, changing subscriptions on the fly should be banned. ⚠️
83-
const flattenedSubscription = flattenSubscription(subscription)
8481
React.useEffect(() => {
8582
// We have rendered, so all fields are no registered, so we can unpause validation
8683
form.isValidationPaused() && form.resumeValidation()
@@ -103,7 +100,7 @@ const ReactFinalForm = ({
103100
unsubscriptions.forEach(unsubscribe => unsubscribe())
104101
}
105102
// eslint-disable-next-line react-hooks/exhaustive-deps
106-
}, [decorators, flattenedSubscription, state])
103+
}, [decorators])
107104

108105
// warn about decorator changes
109106
// istanbul ignore next

src/ReactFinalForm.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React from 'react'
2-
import { render, fireEvent, cleanup } from 'react-testing-library'
2+
import { render, fireEvent, cleanup, act } from 'react-testing-library'
33
import 'jest-dom/extend-expect'
44
import deepEqual from 'fast-deep-equal'
55
import { ErrorBoundary, Toggle, wrapWith } from './testUtils'

src/flattenSubscription.js

Lines changed: 0 additions & 11 deletions
This file was deleted.

src/flattenSubscription.test.js

Lines changed: 0 additions & 15 deletions
This file was deleted.

src/useField.js

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// @flow
22
import * as React from 'react'
33
import { fieldSubscriptionItems } from 'final-form'
4-
import flattenSubscription from './flattenSubscription'
54
import type { FieldSubscription, FieldState, FormApi } from 'final-form'
65
import type { UseFieldConfig, FieldInputProps, FieldRenderProps } from './types'
76
import isReactNative from './isReactNative'
@@ -82,8 +81,6 @@ const useField = (
8281
return beforeSubmit && beforeSubmit()
8382
}
8483

85-
// In the future, changing subscriptions on the fly should be banned. ⚠️
86-
const flattenedSubscription = flattenSubscription(subscription)
8784
React.useEffect(
8885
() =>
8986
register(state => {
@@ -102,9 +99,7 @@ const useField = (
10299
// validate,
103100
initialValue,
104101
isEqual,
105-
validateFields,
106-
// eslint-disable-next-line react-hooks/exhaustive-deps
107-
flattenedSubscription
102+
validateFields
108103
]
109104
)
110105

src/useFormState.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
// @flow
22
import * as React from 'react'
3-
import flattenSubscription from './flattenSubscription'
43
import type { UseFormStateParams } from './types'
54
import type { FormState, FormApi } from 'final-form'
65
import { all } from './ReactFinalForm'
@@ -27,8 +26,6 @@ const useFormState = ({
2726
}
2827
)
2928

30-
// In the future, changing subscriptions on the fly should be banned. ⚠️
31-
const flattenedSubscription = flattenSubscription(subscription)
3229
React.useEffect(
3330
() =>
3431
form.subscribe(newState => {
@@ -42,7 +39,7 @@ const useFormState = ({
4239
}
4340
}, subscription),
4441
// eslint-disable-next-line react-hooks/exhaustive-deps
45-
[flattenedSubscription]
42+
[]
4643
)
4744
return state
4845
}

0 commit comments

Comments
 (0)