gpt4 book ai didi

react-final-form - 如何使用带有react-final-form的多个提交按钮?

转载 作者:行者123 更新时间:2023-12-03 15:29:00 27 4
gpt4 key购买 nike

我想使用具有多个提交按钮的react-final-form构建表单,其中每个提交按钮在表单中设置不同的值。本质上,我想创建一个在呈现的HTML中看起来像这样的表单:

<form>
Are you over 18 years old?
<button type="submit">Yes</button>
<button type="submit">No</button>
</form>

但是,我不知道如何使react-final-form将这些不同的提交按钮视为表单中的设置值。我尝试结合组件状态, <input type="hidden">onClick处理程序,如下所示:

class FormWithMultipleSubmits extends React.Component {
state = {
value: undefined
};

setValue = value => this.setState({ value: value });

render() {
return (
<Form>
Are you over 18 years old?
<Field
name="adult"
component="input"
type="hidden"
value={this.state.value}
/>
<button type="submit" onClick={() => this.setValue(true)}>
Yes
</button>
<button type="submit" onClick={() => this.setValue(false)}>
No
</button>
</Form>
);
}
}

但是,这似乎不起作用-可能是因为 the value property on the <Field> component is only used for checkboxes and radio buttons

有人可以按正确的方向向我推一下,以解决该问题吗?

最佳答案

这是一个Sandbox,显示了如何。

关于react-final-form - 如何使用带有react-final-form的多个提交按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51839138/

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