我想用
react-final-form
它有多个提交按钮,其中每个提交按钮在表单中设置不同的值。本质上,我想创建一个在呈现的HTML中类似这样的表单:
<form>
Are you over 18 years old?
<button type="submit">Yes</button>
<button type="submit">No</button>
</form>
但是,我不知道如何使React最终表单将这些不同的提交按钮作为表单中的设置值。我尝试组合组件状态,
<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
.
有人能把我推向正确的方向吗?怎样才能正确地解决这个问题?