-
Notifications
You must be signed in to change notification settings - Fork 2
Consider providing access to state context in onSubmit #6
Copy link
Copy link
Open
Description
A common pattern is to need to update arbitrary field values while handling a submit response.
Currently this can be achieved with a ref to the Form component that shares a context with the onSubmit callback:
class CustomForm extends React.PureComponent {
formRef = React.createRef();
render() {
return (
<Form
{...this.props}
onSubmit={(values) => {
return sendFetch(values).then((response) => {
if (response.hasSomeCondition) {
formRef.current.setValue('field', value);
return Promise.reject(new SubmitValidationError(errors));
}
}};
}}
/>
)
}
}It would be much cleaner to provide access to the state context through additional arguments of onSubmit:
const CustomForm = () => (
<Form
{...this.props}
onSubmit={(values, context) => {
return sendFetch(values, context).then((response) => {
if (response.hasSomeCondition) {
context.setValue('field', value);
return Promise.reject(new SubmitValidationError(errors));
}
}};
}}
/>
);Questions:
- Should callback props other than
onSubmitalso have access to the context? - What part of, if not the entire context, should be provided?
- Is the first
valuesargument necessary if the full context is provided? Thevaluesarg andcontext.valueStatewould be the same value. Omitting the first arg and requiring the consumer to accessvalueStateon context doesn't limit functionality, and arguably makes for a "simpler" (but less obvious) interface. - A static reference to
contextas it exists at the timeonSubmitis called may be outdated by the time it is referenced. Is agetContextarg that would provide access to the latest updated context when needed a better choice?
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels