简体   繁体   中英

How Can I Get Form/Submit To Work with ReactStrap in a React functional component?

I like how Reactstrap handles Modal so I want to keep using it, but I can't figure out how to get the data out of a form and capture it in state.

const handleSubmit = (evt) => {
    evt.preventDefault();
    alert(`Submitting Name ${name}`);
};

With Reactstrap

<Form onSubmit={handleSubmit}>
   <FormGroup>
      <Label for="firstname">First Name</Label>{' '}
      <Input name="speakername"></Input>
    </FormGroup>
</Form>
    

When I use standard form and input elements, I'm able to capture what I need in handleSubmit, but I can't figure out how to do the same thing with the Form and Input tags of Reactstrap

Regular form and input elements

<form onSubmit={handleSubmit}>
    <label>
        First Name:
        <input
            type="text"
            value={name}
            onChange={e => setName(e.target.value)}
        />
    </label>
    <input type="submit" value="Submit" />
</form>

Add a Button component with type=submit to your reactstrap form the same way you had an <input> with type=submit so that React know's to fire the onSubmit handler when the Button is clicked.

import { Form, FormGroup, Input, Label, Button } from "reactstrap";

<Form onSubmit={handleSubmit}>
   <FormGroup>
      <Label for="firstname">First Name</Label>{' '}
      <Input name="speakername"></Input>
    </FormGroup>
    <Button type="submit">Submit</Button>
</Form>

I was having exactly the same problem. Seemed to have fixed it as follows...

(I believe all you're missing are the value & onChange props for the Input component, and possibly the useState hooks for setName()... )

--- Set state ---

const currentDate = findDate();

function findDate() {
  let d = new Date(),
    month = "" + (d.getMonth() + 1),
    day = "" + d.getDate(),
    year = d.getFullYear();

  if (month.length < 2) month = "0" + month;
  if (day.length < 2) day = "0" + day;

  return [year, month, day].join("-");
}

console.log(typeof currentDate);

const UpdateCount = () => {
  const [date, setDate] = useState(currentDate);
  const [hactCount, setHactCount] = useState("");

--- Handle Submit function ---

    const handleSubmit = (e) => {
      e.preventDefault();    
      alert(`${hactCount} hacts on ${date}`);
    };

--- Return from functional component ---

return (
    <div>
      <Card>
        <CardTitle className="border-bottom p-3 mb-0">
          <i className="mdi mdi-priority-low mr-2"></i>Update your Hact Count
        </CardTitle>
        <CardBody>
          <CardSubtitle className="py-2">
            Insert your day's count and we'll do the magic
          </CardSubtitle>

          <Form onSubmit={handleSubmit}>
            <FormGroup>
              Date:
              <Input
                className="mt-2 mb-4"
                type="date"
                value={date}
                onChange={(e) => {
                  setDate(e.target.value);
                  console.log(typeof e.target.value);
                }}
              />
              Count:
              <Input
                className="my-2 mb-4"
                type="number"
                placeholder="0"
                value={hactCount}
                onChange={(e) => {
                  setHactCount(e.target.value);
                  console.log(e.target.value);
                }}
              />                 
              <br />
              <InputGroup className="text-center">
                <Button className="text-center" color="primary" type="submit">
                  Update
                </Button>
              </InputGroup>
            </FormGroup>
          </Form>
        </CardBody>
      </Card>

    </div>
  );

you should be able using innerRef

 onFormSubmit = (e) => {
    e.preventDefault()
    console.log(this.emailInputValue)
}

<Form onSubmit={this.onFormSubmit}>
                <FormGroup>
                    <Label >Email:</Label>
                    <Input innerRef={(node) => this.emailInputValue = node} type="email" name="email" " placeholder="Email" />
         
                <Button type="submit" color="primary"  >Submit</Button>
</Form>

You can get the value by referring to the name key and ID of the input.

  • Exmaple
 onFormSubmit = (e) => {
    e.preventDefault()
    console.log(e.target.company.value)
    console.log(e.target.name.value)
}
  <Modal isOpen={isModalVisible} toggle={handleModal}>
            <ModalHeader toggle={handleModal}>add modal</ModalHeader>
            <ModalBody>
                <Form onSubmit={onFinish}>
                    <FormGroup>
                        <Label for="company">company</Label>
                        <Input type={"text"} name={"company"} id={"company"} placeholder={"company"}  />
                    </FormGroup>
                    <FormGroup>
                        <Label for="name">name</Label>
                        <Input type={"text"} name={"name"} id={"name"} placeholder={"name"} />
                    </FormGroup>
                    <Button type="submit" color={"primary"}>
                        save
                    </Button>
                </Form>
            </ModalBody>
        </Modal>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM