反应按钮不能有两个箭头功能?
A react button cannot have two arrow functions?
我正在尝试制作一个显示带有表单的 bootstrap 模式的按钮,并用您单击的元素的数据填充它(该应用程序允许您列出、添加、删除和编辑元素) .问题是 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>
</>
)
}
你不应该在功能组件中使用状态,而是使用钩子,然后组合两个函数
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>
</>
);
}
我正在尝试制作一个显示带有表单的 bootstrap 模式的按钮,并用您单击的元素的数据填充它(该应用程序允许您列出、添加、删除和编辑元素) .问题是 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>
</>
)
}
你不应该在功能组件中使用状态,而是使用钩子,然后组合两个函数
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>
</>
);
}