gpt4 book ai didi

forms - react : how to use child FormItem components without getting Warning: validateDOMNesting:
cannot appear as a descendant of

转载 作者:行者123 更新时间:2023-12-03 13:26:58 24 4
gpt4 key购买 nike

给定父组件,我正在使用子组件 DynamicFieldSet这是一组FormItems 。但我收到错误:

Warning: validateDOMNesting(...): <form> cannot appear as a descendant of <form>. See CreateTopic > Form > form > ... > DynamicFieldSet > Form > form.

我尝试删除 <Form> </Form>我的子组件中的标签,但这是一个编译错误。

有没有办法可以禁用子表单 View 的渲染?

父组件

class CreateTopic extends React.Component {
render() {
return (
<div className="create-topic-container">
<h3>Create an event</h3>
<Form onSubmit={this.handleSubmit}>
<FormItem>...</FormItem>
<FormItem>...</FormItem>
<FormItem>...</FormItem>
<FormItem
{...formItemLayout}
label="Results"
style={{ marginBottom: SPACING_FORM_ITEM }}
>
{getFieldDecorator('results', {
rules: [
{
required: true,
message: 'Results cannot be empty.',
},
],
})(<DynamicFieldSet
form={this.props.form}
/>)}
</FormItem>
</Form>
</div>
);
}
}

DynamicFieldSet - 子组件

export class DynamicFieldSet extends React.Component {
render() {
getFieldDecorator('keys', { initialValue: ['0', '1'] });
const keys = getFieldValue('keys');
const formItems = keys.map((k, index) => {
return (
<FormItem
{...formItemLayoutWithOutLabel}
required={false}
key={k}
>
{getFieldDecorator(`results[${k}]`, {
validateTrigger: ['onChange', 'onBlur'],
rules: [
{
required: true,
whitespace: true,
message: 'Result name cannot be empty.',
},
{
validator: this.validateLength,
},
],
})(<Input placeholder={`Result #${index + 1}`} style={{ width: '80%', marginRight: 8 }} />)}
{keys.length > 2 ? (
<Icon
className="dynamic-delete-button"
type="minus-circle-o"
disabled={keys.length === 1}
onClick={() => this.remove(k)}
/>
) : null}
</FormItem>
);
});

return (
<Form>
{formItems}
<FormItem {...formItemLayoutWithOutLabel}>
{keys.length < 10 ? (
<Button type="dashed" onClick={this.add} style={{ width: '80%' }}>
<Icon type="plus" />Add Result
</Button>
) : null}
</FormItem>
</Form>
);
}
}

最佳答案

我在使用ant design table时遇到了这个问题,结果发现它不是引发警告的ant design。这是网络标准description

“每个表单都必须包含在 FORM 元素内。单个文档中可以有多个表单,但 FORM 元素不能嵌套。”

因此,表单标签内不应该有表单标签。

要解决该问题(在我们的例子中),请删除 DynamicFieldSet“return”内的 Form 标记,并替换为 div 标记

希望有帮助:)

关于forms - react : how to use child FormItem components without getting Warning: validateDOMNesting: <form> cannot appear as a descendant of <form>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48619381/

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