【发布时间】:2021-10-26 07:43:59
【问题描述】:
我目前使用 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
}
【问题讨论】:
标签: reactjs react-bootstrap fetch-api mern