【问题标题】:A react button cannot have two arrow functions?一个反应按钮不能有两个箭头功能?
【发布时间】:2020-01-09 08:50:02
【问题描述】:

我正在尝试制作一个按钮,该按钮显示带有表单的引导模式,并用您单击的元素的数据填充它(该应用程序允许您列出、添加、删除和编辑元素)。问题是只有 onclick 事件中的第二个箭头函数有效。我曾尝试将它们放在一个函数中,但我仍然无法弄清楚。所以这里是代码,我仍然是反应的初学者,所以我希望你能帮助我。

editButton(celldata) {

        const [show, setShow] = useState(false);

        const handleClose = () => setShow(false);
        const handleShow = () => setShow(true);

        return (
            <>
            <button
                type="button"
                onClick={() => this.setState({ cellName: celldata.celldata.cell, case: celldata.celldata.case, comments: celldata.celldata.comments }), handleShow}
                className="btn btn-outline-dark">
                    <Edit/>
            </button>
            {console.log(this.state.case, show)}
            <Modal show={show} onHide={handleClose}>
                <Modal.Header closeButton>
                <Modal.Title>Edit Cell</Modal.Title>
                </Modal.Header>
                <Modal.Body>
                    <Form>
                        <Form.Group>
                            <Form.Label>Cell</Form.Label>
                            <Form.Control value={this.state.cellName} onChange={(e) => this.cellNameHandler(e)} style={{textAlign: 'left'}} placeholder="Cell Name" />
                        </Form.Group>
                        <Form.Group>
                            <Form.Label>Use Case</Form.Label>
                            <Form.Control value={this.state.case} onChange={(e) => this.caseHandler(e)} as="select">
                                {
                                    use_cases.use_cases.map((u) =>
                                        <option>
                                            {u}
                                        </option>
                                    )
                                }
                            </Form.Control>
                        </Form.Group>
                        <Form.Group>
                            <Form.Label>Comments</Form.Label>
                            <Form.Control value={this.state.comments} onChange={(e) => this.commentsHandler(e)} style={{textAlign: 'left'}} as="textarea" rows="3" />
                        </Form.Group>
                        <Button variant="secondary" onClick={handleClose}>
                            Cancel
                        </Button>
                        <Button variant="primary" onClick={() => { this.handleSave() }}>
                            Add
                        </Button>
                    </Form>
                </Modal.Body>
            </Modal>
            </>
        )
    }

【问题讨论】:

  • 您在功能组件中使用this.setState?那是针对类组件的。在功能组件中拥有状态的唯一方法是通过钩子

标签: reactjs react-bootstrap react-bootstrap4-modal


【解决方案1】:

你不应该在函数组件中使用状态,而是使用钩子,然后组合两个函数

function editButton(celldata) {
  const [show, setShow] = useState(false);
  const [selectedData, setSelectedData] = useState({});

  const handleClose = () => setShow(false);
  const handleShow = () => setShow(true);
  const handleSelect = () => {
    setSelectedData({
      cellName: celldata.celldata.cell,
      case: celldata.celldata.case,
      comments: celldata.celldata.comments
    });
  };
  const handleButtonClick = () => {
    handleSelect();
    handleShow();
  };
  return (
    <>
      <button
        type="button"
        onClick={handleButtonClickhandleButtonClick}
        className="btn btn-outline-dark"
      >
        <Edit />
      </button>
      {console.log(selectedData.case, show)}
      <Modal show={show} onHide={handleClose}>
        <Modal.Header closeButton>
          <Modal.Title>Edit Cell</Modal.Title>
        </Modal.Header>
        <Modal.Body>
          <Form>
            <Form.Group>
              <Form.Label>Cell</Form.Label>
              <Form.Control
                value={selectedData.cellName}
                onChange={e => this.cellNameHandler(e)}
                style={{ textAlign: 'left' }}
                placeholder="Cell Name"
              />
            </Form.Group>
            <Form.Group>
              <Form.Label>Use Case</Form.Label>
              <Form.Control
                value={selectedData.case}
                onChange={e => this.caseHandler(e)}
                as="select"
              >
                {use_cases.use_cases.map(u => (
                  <option>{u}</option>
                ))}
              </Form.Control>
            </Form.Group>
            <Form.Group>
              <Form.Label>Comments</Form.Label>
              <Form.Control
                value={selectedData.comments}
                onChange={e => this.commentsHandler(e)}
                style={{ textAlign: 'left' }}
                as="textarea"
                rows="3"
              />
            </Form.Group>
            <Button variant="secondary" onClick={handleClose}>
              Cancel
            </Button>
            <Button
              variant="primary"
              onClick={() => {
                this.handleSave();
              }}
            >
              Add
            </Button>
          </Form>
        </Modal.Body>
      </Modal>
    </>
  );
}

【讨论】:

  • 这对我有用,现在数据可以在表单中正确显示,但我无法修改任何字段,我是否需要另一个函数,如 onchange 事件的句柄选择?
猜你喜欢
  • 2019-06-25
  • 1970-01-01
  • 2023-03-24
  • 2018-12-14
  • 2021-05-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多