不能取react-bootstrap组件事件目标值
Can't take react-bootstrap component event target value
我目前使用 react-bootstrap (4.6) 库来渲染代码。到目前为止,我已经绘制出一个数组,该数组从我的数据库中给出 eachProdcut
。但是,我尝试在单击时访问该值,但它向我发送了 undefined
或预期的值 _id
.
我尝试使用事件池 (https://reactjs.org/docs/legacy-event-pooling.html#gatsby-focus-wrapper) 中的 .persist()
。虽然,我有时仍然会收到 undefined
值。
至于 onClick
事件,我尝试传递通常的箭头函数来处理事件 event => select(event)
但没有成功。
(代码) ID 试图访问:
<ListGroup.Item action onClick={select} className="product-item" key={eachProduct._id} value={eachProduct._id}>
<Row>
<Col>
{eachProduct.name}
</Col>
<Col className="text-right">
{eachProduct.isActive ?
<strong className="text-success">Active</strong>:
<strong className="text-danger">Inactive</strong>}
</Col>
</Row>
</ListGroup.Item>
(code) 函数尝试获取 id:
const select = (event) => {
event.persist()
console.log(event.target.value) // returning either undefined or expected output id
}
使用 event.currentTarget.value
而不是 event.target.value
后似乎一切正常
我目前使用 react-bootstrap (4.6) 库来渲染代码。到目前为止,我已经绘制出一个数组,该数组从我的数据库中给出 eachProdcut
。但是,我尝试在单击时访问该值,但它向我发送了 undefined
或预期的值 _id
.
我尝试使用事件池 (https://reactjs.org/docs/legacy-event-pooling.html#gatsby-focus-wrapper) 中的 .persist()
。虽然,我有时仍然会收到 undefined
值。
至于 onClick
事件,我尝试传递通常的箭头函数来处理事件 event => select(event)
但没有成功。
(代码) ID 试图访问:
<ListGroup.Item action onClick={select} className="product-item" key={eachProduct._id} value={eachProduct._id}>
<Row>
<Col>
{eachProduct.name}
</Col>
<Col className="text-right">
{eachProduct.isActive ?
<strong className="text-success">Active</strong>:
<strong className="text-danger">Inactive</strong>}
</Col>
</Row>
</ListGroup.Item>
(code) 函数尝试获取 id:
const select = (event) => {
event.persist()
console.log(event.target.value) // returning either undefined or expected output id
}
使用 event.currentTarget.value
而不是 event.target.value