Skip to content

Instantly share code, notes, and snippets.

@rosschapman
Last active April 22, 2020 22:42
Show Gist options
  • Save rosschapman/9fec302abe53ff94faa4501231a137d9 to your computer and use it in GitHub Desktop.
Save rosschapman/9fec302abe53ff94faa4501231a137d9 to your computer and use it in GitHub Desktop.
Weakly Bound Form Callback
// This is oversimplified. The real code for this callback would be a complicated graph
// of nested asynchronous and synchronous calls. Imagine at the edge of thes thunks each
// dispatched action mutates state.
let postEntityForm = (e, data) => {
await dispatch(saveEntity(data));
let entities = await dispatch(fetchEntities());
let taxPolicy = await dispatch(maybeFetchEntityTaxPolicy());
await dispatch(maybeUpdateEntityPriceSuggestions(taxPolicy, entities));
let isEditing = dispatch(getIsEditingFromState());
if (isEditing) {
dispatch(prePopulateForm(data));
} else {
dispatch(resetForm());
}
}
let MyFormComponent = () => {
return {
<Form>
<Button type={'submit'} onClick={postEntityForm}/>
</Form>
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment