如何在 react-final-form 中将 null 作为字段的初始值传递?
How to pass null as a field's initial value in react-final-form?
我在 react-final-form
<Field>
中使用的自定义 React 组件具有 int?
的价值支柱。它的值可以是整数或空值。但是当我使用 <Form>
组件的 initialValues
属性为此字段组件的值设置 null
初始值时,react-final-form
将 null
转换为空字符串''
.
我知道我可以通过创建一个检查 ''
并将其转换为 null
的包装器组件轻松解决此问题,但是是否有其他更简洁的方法来解决此问题?或者这是一个库错误?
https://codesandbox.io/s/yq00zxn271
import React from "react";
import { render } from "react-dom";
import { Form, Field } from "react-final-form";
const IntField = (props) => (
<span>
<input type="text" value={props.value === null ? 0 : props.value} />
<pre>
<b>props.{props.name} === null</b> : {(initialValues[props.name] === null).toString()}
<br />
<b>props.value === null</b> : {((isNull) => (<span style={{ color: isNull ? 'green' : 'red' }}>{isNull.toString()}</span>))(props.value === null)}
<br />
<b>props: </b>{JSON.stringify(props)}
<br />
<b>initialValues: </b>{JSON.stringify(initialValues)}
</pre>
</span>
)
const onSubmit = values => {
console.log('submitted')
}
const initialValues = { someInteger: null };
const App = () => (
<Form
initialValues={initialValues}
onSubmit={onSubmit}
render={() => (
<form>
<label>Some Integer:</label>
<Field name="someInteger">
{({ input, meta }) => (
<IntField {...input} />
)}
</Field>
</form>
)}
/>
);
render(<App />, document.getElementById("root"));
您可以使用 <Field>
属性 allowNull? 关闭此行为,以便将空值按原样传递给子组件。
像这样:
<Field name="someInteger1" allowNull={true}>
{({ input, meta }) => (
<IntField {...input} />
)}
</Field>
https://codesandbox.io/s/n4rl2j5n04
(回答我自己的问题是为了帮助其他人,因为我花了一段时间才弄清楚发生了什么以及如何解决它)
如果你像我一样正在寻找一种方法来防止最终形式取消定义空字符串,你可以使用 parse 和恒等函数:
<Field
name="myField"
parse={x => x}
component={TextField}
/>
我在 react-final-form
<Field>
中使用的自定义 React 组件具有 int?
的价值支柱。它的值可以是整数或空值。但是当我使用 <Form>
组件的 initialValues
属性为此字段组件的值设置 null
初始值时,react-final-form
将 null
转换为空字符串''
.
我知道我可以通过创建一个检查 ''
并将其转换为 null
的包装器组件轻松解决此问题,但是是否有其他更简洁的方法来解决此问题?或者这是一个库错误?
https://codesandbox.io/s/yq00zxn271
import React from "react";
import { render } from "react-dom";
import { Form, Field } from "react-final-form";
const IntField = (props) => (
<span>
<input type="text" value={props.value === null ? 0 : props.value} />
<pre>
<b>props.{props.name} === null</b> : {(initialValues[props.name] === null).toString()}
<br />
<b>props.value === null</b> : {((isNull) => (<span style={{ color: isNull ? 'green' : 'red' }}>{isNull.toString()}</span>))(props.value === null)}
<br />
<b>props: </b>{JSON.stringify(props)}
<br />
<b>initialValues: </b>{JSON.stringify(initialValues)}
</pre>
</span>
)
const onSubmit = values => {
console.log('submitted')
}
const initialValues = { someInteger: null };
const App = () => (
<Form
initialValues={initialValues}
onSubmit={onSubmit}
render={() => (
<form>
<label>Some Integer:</label>
<Field name="someInteger">
{({ input, meta }) => (
<IntField {...input} />
)}
</Field>
</form>
)}
/>
);
render(<App />, document.getElementById("root"));
您可以使用 <Field>
属性 allowNull? 关闭此行为,以便将空值按原样传递给子组件。
像这样:
<Field name="someInteger1" allowNull={true}>
{({ input, meta }) => (
<IntField {...input} />
)}
</Field>
https://codesandbox.io/s/n4rl2j5n04
(回答我自己的问题是为了帮助其他人,因为我花了一段时间才弄清楚发生了什么以及如何解决它)
如果你像我一样正在寻找一种方法来防止最终形式取消定义空字符串,你可以使用 parse 和恒等函数:
<Field
name="myField"
parse={x => x}
component={TextField}
/>