如何访问父组件中的窗体状态?
这就是我所做的(只是一个简单的代码,请忽略语法)
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>
}现在的问题是,如果子程序在onSelect处理程序()中是脏的,我无法通知父程序不呈现子对象。我不能在呈现方法中执行setState,至少我可以提前使用componentDidUpdate通知
发布于 2019-07-30 15:25:23
从第551期复制
另一种可能是,最近有一个API将允许您提供自己的表单实例。更改为<Form>,所以类似这样的东西可以工作:
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>
)
}https://stackoverflow.com/questions/57265415
复制相似问题