From 8fc43578fd6a5d161b2d934e431ba9fdd6204089 Mon Sep 17 00:00:00 2001 From: Umidbek Karimov Date: Wed, 23 Jan 2019 21:07:18 +0500 Subject: [PATCH] fix(useField): Resubscribe only on subscription props change. (#16) --- src/useField.js | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/useField.js b/src/useField.js index 5b958de..d2fbac4 100644 --- a/src/useField.js +++ b/src/useField.js @@ -6,6 +6,9 @@ export const all = fieldSubscriptionItems.reduce((result, key) => { return result }, {}) +const subscriptionToInputs = subscription => + fieldSubscriptionItems.map(key => Boolean(subscription[key])) + const eventValue = event => { if (!event.target) { return event @@ -16,7 +19,7 @@ const eventValue = event => { return event.target.value } -const useField = (name, form, subscription) => { +const useField = (name, form, subscription = all) => { const autoFocus = useRef(false) const [state, setState] = useState({}) useEffect( @@ -30,9 +33,9 @@ const useField = (name, form, subscription) => { } setState(newState) }, - subscription || all + subscription ), - [name, subscription] + [name, ...subscriptionToInputs(subscription)] ) let { blur, change, focus, value, ...meta } = state || {} delete meta.name // it's in input