我怎样才能 Form/Submit 在 React 功能组件中使用 ReactStrap?

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

我喜欢 Reactstrap 的处理方式 Modal 所以我想继续使用它,但我不知道如何从表单中获取数据并在状态中捕获它。

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

带 Reactstrap

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

当我使用标准表单和输入元素时,我能够在 handleSubmit 中捕获我需要的内容,但我不知道如何使用 Reactstrap 的表单和输入标签做同样的事情

常规表格和输入元素

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

你应该可以使用 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>

将带有 type=submitButton 组件添加到您的 reactstrap 表单中,就像您使用带有 type=submit<input> 一样,以便 React 知道要触发单击 Button 时的 onSubmit 处理程序。

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>

我遇到了完全相同的问题。似乎已按如下方式修复它...

(我相信您所缺少的只是 Input 组件的 value 和 onChange 道具,可能还有 setName() 的 useState 挂钩...)

---设置状态---

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("");

--- 处理提交功能 ---

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

--- Return 来自功能组件 ---

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

可以通过输入的name键和ID来获取值

  • 例子
 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>