How do I access form state in the parent component
This is what I am doing (just a brief code, ignore the syntax please)
class Parent {
<listComponent
onSelect: handler
>
handler() {
// Do this only if the already opened ChildComp in not dirty
<ChildComp>
}
}
// Uses react-final-form
class ChildComp {
<form
onSubmit: handleSubmit
render: renderForm
>
renderForm ({dirty}){
// Assigning to a class variable and prompting for unsaved changes which I am able to do
this.isFormDirty = dirty
return(
<InputField>
);
}
</form>
}
The problem now is, I am not able to notify the parent not to render the child if the child is dirty in onSelect handler(). I cannot do setState in the render method, at least I could have notified using componentDidUpdate Thanks in advance
Copied from Issue #551:
Another possibility is that there's a recent API change that lets you provide your own form instance to <Form>
, so something like this could work:
import { createForm } from 'final-form'
function TestForm() {
const formRef = React.useRef(createForm({
onSubmit: myOnSubmit
})
return (
<div>
<Form form={formRef.current}>
{({ handleSubmit, form }) => (
<form onSubmit={handleSubmit}> ... fields ... </form>
)}
</Form>
<button onClick={() => formRef.current.reset()}>Reset</button>
</div>
)
}