Why use listeners and not just onChange
?
#1046
-
Hey! I would like to know about the intended use case of the "listeners" API. "Country" input changes, "Province" field is resetted The example has a listener on the "Country" field: <form.Field
name="country"
listeners={{
onChange: ({ value }) => {
console.log(`Country changed to: ${value}, resetting province`);
form.setFieldValue("province", "");
},
}}
>
{(field) => (
<label>
<div>Country</div>
<input
value={field.state.value}
onChange={(e) => field.handleChange(e.target.value)}
/>
</label>
)}
</form.Field> But why not just execute this update in the <input
value={field.state.value}
onChange={e => {
field.handleChange(e.target.value)
form.setFieldValue("province", "")
}}
/> Is it just to have a cleaner separation of concerns or are the other benefits in using the "listener"? |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 2 replies
-
I feel listeners API is more useful in cases where we want to trigger event handlers of field based on other field's value change. |
Beta Was this translation helpful? Give feedback.
-
I think it separates the concerns from the basic onChange updating the form with the new value and any side effects that should occur. On our project that contains complex validations it's a lifesaver. |
Beta Was this translation helpful? Give feedback.
-
It's really cool seeing people discuss the api 🤟 Well as you mentioned the separation of concerns, but also because onChange isn't the only event you can subscribe to.
All those events can be subscribed to in the listeners api, could you imagine what your component would look like trying to subscribe to these events... And as everyone else has mentioned, handleChange should handle the change of the field value, its nice to be able to separate out the logic when you have dependent fields. It also allows you to abstract away your components from the form field logic, so your NumberInput component doesn’t need to account for how it relates to other form fields, thats now the job of Field. this <Field
name="age"
children={(handleChange, state, ...// rest of props ) => (
<NumberInput
state={state}
handleChange={(e) => {
field.handleChange(e.target.value)
form.setFieldValue("permissions", [])
}}
// rest of props
/>
)}
/> can become <Field
name="age"
listeners={{
onChange: ({ value }) => {
setFieldValue('permissions', []);
},
}}
children={(props) => (
<NumberInput props={props} />
)}
/> But, that's more subjective, personally I prefer it. Hope this helps explain the reasoning! |
Beta Was this translation helpful? Give feedback.
It's really cool seeing people discuss the api 🤟
Well as you mentioned the separation of concerns, but also because onChange isn't the only event you can subscribe to.
All those events can be subscribed to in the listeners api, could you imagine what your component would look like trying to subscribe to these events... And as everyone else has mentioned, handleChange should handle the change of the field value, its nice to be able to separate out the logic when you have dependent fields.
It also allows you to abstract away your components from the form field logic, so your NumberInput component doesn’t need to account for how it relates to other form fie…