![](/img/trans.png)
[英]Warning: validateDOMNesting(…): <form> cannot appear as a descendant of <form>
[英]Warning: validateDOMNesting(…): <form> cannot appear as a descendant of <form> by using semantic-ui-react modal
當我在semantic-ui-react的模態中使用Form
時,它會顯示該錯誤。
警告:
validateDOMNesting(…)
: 不能作為后代出現
我知道如果表格中有表格,那就是表演。
下面是我的代碼,沒有人。 如果我不使用模態,則沒有錯誤。
import { useState } from "react";
import { Helmet } from "react-helmet";
import { Button, Modal, Form } from "semantic-ui-react";
import { Body, Wrapper, Content, Article } from "../../Styles/Wrapper";
// eslint-disable-next-line import/no-anonymous-default-export
export default (company_id, company_secret, onSubmit) => {
const [open, setOpen] = useState(false);
return (
<Body>
<Wrapper>
<Helmet>
<title>juju</title>
</Helmet>
<Content>
<Article>
<Modal as={Form}
onClose={() => setOpen(false)}
onOpen={() => setOpen(true)}
open={open}
trigger={
<Button
style={{ marginBottom: 10, backgroundColor: "#FEE500" }}
size="large"
fluid
>
<span style={{ fontSize: 15 }}>begin</span>
</Button>
}
>
<Modal.Header>add</Modal.Header>
<Modal.Content>
<Form onSubmit={onSubmit}>
<Form.Group>
<Form.Input
placeholder="put id"
name="id"
{...company_id}
/>
<Form.Input
placeholder="put secret"
name="secret"
{...company_secret}
/>
<Form.Button content="Submit" />
</Form.Group>
</Form>
</Modal.Content>
</Modal>
</Article>
</Content>
</Wrapper>
</Body>
);
};
form
中不能有form
。 渲染Modal
組件時移除as={Form}
。 您還應該修復 function arguments,因為該組件接收props
object。 您應該解構company_id
、 company_secret
和onSubmit
。
export default ({ company_id, company_secret, onSubmit }) => {
// ...
}
<Form.Input>
組件存在一些問題。 您應該將value
和onChange
道具傳遞給他們。 您可以創建幾個 state 變量companyId
和companySecret
來管理輸入狀態。
const [companyId, setCompanyId] = useState(company_id)
const [companySecret, setCompanySecret] = useState(company_secret)
<>
<Form.Input
name="id"
value={companyId}
onChange={(e) => setCompanyId(e.target.value)}
/>
<Form.Input
name="secret"
value={companySecret}
onChange={(e) => setCompanySecret(e.target.value)}
/>
</>
PS 我建議在任何地方使用camelCase
變量(除非你絕對必須使用snake_case
)以保持一致性。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.