我需要在onSubmit方法中获取所有值,而不仅仅是脏字段。
import React from 'react'
import { Form, Field } from 'react-final-form'
const App: React.FC = () => (
<Form onSubmit={values => console.log(values)}>
{({ form: { submit } }) => (
<>
<Field name="street" component="input" placeholder="street" />
<Field name="city" component="input" placeholder="city" />
<Field name="state" component="input" placeholder="state" />
<button onClick={submit}>Submit</button>
</>
)}
</Form>
)
export default App
实际结果:
{street: "A", city: "B"}
预期结果:
{street: "A", city: "B", state: null}
答案 0 :(得分:0)
?最终表格treats ''
and undefined
as more or less equivalent。
您需要提供initialValues={{ street: null, city: null, state: null }}
才能获得预期的结果。但是,如果用户触摸字段,更改值,然后将其更改回空,则street
键将从表单值中删除(请参见上面的链接)。您可以通过提供parse={v => v}
to cancel the normal ''
-to-undefined
conversion来解决这个问题。
希望有帮助吗?