Skip to content

Commit 8fc4357

Browse files
avocadowastakenerikras
authored andcommitted
fix(useField): Resubscribe only on subscription props change. (#16)
1 parent 12d86f3 commit 8fc4357

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

src/useField.js

+6-3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ export const all = fieldSubscriptionItems.reduce((result, key) => {
66
return result
77
}, {})
88

9+
const subscriptionToInputs = subscription =>
10+
fieldSubscriptionItems.map(key => Boolean(subscription[key]))
11+
912
const eventValue = event => {
1013
if (!event.target) {
1114
return event
@@ -16,7 +19,7 @@ const eventValue = event => {
1619
return event.target.value
1720
}
1821

19-
const useField = (name, form, subscription) => {
22+
const useField = (name, form, subscription = all) => {
2023
const autoFocus = useRef(false)
2124
const [state, setState] = useState({})
2225
useEffect(
@@ -30,9 +33,9 @@ const useField = (name, form, subscription) => {
3033
}
3134
setState(newState)
3235
},
33-
subscription || all
36+
subscription
3437
),
35-
[name, subscription]
38+
[name, ...subscriptionToInputs(subscription)]
3639
)
3740
let { blur, change, focus, value, ...meta } = state || {}
3841
delete meta.name // it's in input

0 commit comments

Comments
 (0)