【问题标题】:Can't take react-bootstrap component event target value无法获取 react-bootstrap 组件事件目标值
【发布时间】: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


    【解决方案1】:

    使用event.currentTarget.value 而不是event.target.value 后似乎一切正常

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-08-26
      • 2020-01-17
      • 2019-02-20
      • 1970-01-01
      • 2020-05-01
      • 2019-12-22
      • 1970-01-01
      相关资源
      最近更新 更多