gpt4 book ai didi

reactjs - 如何使用 React Final Form Field Array 格式化复选框字段的值?

转载 作者:行者123 更新时间:2023-12-04 12:37:29 24 4
gpt4 key购买 nike

在 React Final Form 中,我们可以使用 formatparse Prop text键入输入以存储与指定值不同的值,但这似乎与复选框的工作方式不同。

例子:

<FieldArray name='test' initialValue={[true, false]}>
<Field
component='input'
type='checkbox'
format={value => value ? 'foo' : null}
parse={value => value ? 'foo' : null}/>
</Field>
</FieldArray>

在这个例子中,要存储的值仍然是 truefalse ,不管使用 formatparse .是否可以格式化来自 [true, false] 的值?至 ["foo"] ?

在此先感谢您的帮助。

最佳答案

您的 format函数需要将其转换回 bool 值。

<Field
name="employed"
component="input"
type="checkbox"
format={v => v === "foo"}
parse={v => (v ? "foo" : null)}
/>

但听起来您想要使用复选框来管理数组中的成员资格的内置功能。为此,您只需指定 value支持 <Field/> .请参阅“酱汁”示例:

Edit quirky-voice-zruje

☝️ 还包括 format/ parse例子。

关于reactjs - 如何使用 React Final Form Field Array 格式化复选框字段的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59832895/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com