Search code examples
javascriptreactjssemantic-ui

Warning: validateDOMNesting(…): <form> cannot appear as a descendant of <form> by using semantic-ui-react modal


When I use Form in modal of semantic-ui-react, it shows that error.

Warning: validateDOMNesting(…): cannot appear as a descendant of

I know it is show if there are form in form.

Below is my code, there are no one. if i don't use modal, there are no error.

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>
  );
};

Solution

  • You cannot have a form inside a form. Remove as={Form} when rendering the Modal component. You should also fix the function arguments since the component receives a props object. You should destructure company_id, company_secret, and onSubmit.

    export default ({ company_id, company_secret, onSubmit }) => {
      // ...
    }
    

    And there are a few issues with the <Form.Input> components. You should pass them the value and onChange props. You could create a couple of state variables companyId and companySecret to manage the input states.

    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)}
      />
    </>
    

    Edit semantic-ui-modal-fz1su

    P.S. I would suggest using camelCase variables everywhere (unless you absolutely have to use snake_case) for consistency.