Skip to content

Commit

Permalink
fix(useField): Resubscribe only on subscription props change. (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
avocadowastaken authored and erikras committed Jan 23, 2019
1 parent 12d86f3 commit 8fc4357
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions src/useField.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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(
Expand All @@ -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
Expand Down

0 comments on commit 8fc4357

Please sign in to comment.