【发布时间】: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