开发者问题收集

React 表行上的 OnClick 事件

2019-03-01
11147

大家好,我是 React 新手,第一个问题。我按照这个( React - 在表格行上触发点击事件 )示例在 React 表格​​上设置 onClick 事件,但第一个问题是我在 Visual Studio Code 中收到警告,提示 Uncaught ReferenceError: e 未定义。任何帮助都将不胜感激。

这是我的代码:

import React from 'react';
import './gridStyle.css';

class Grid extends React.Component {

    onClickHandler = () => {
        const song = e.target.getAttribute('data-item');
        console.log('We need to get the details for ', song);
    }

    renderResultRows(data) {
        return data.map((coord, index) =>
            // anon func maintains scope!
            // Pass in a function to our onClick, and make it anon
            // to maintain scope.  The function body can be anything
            // which will be executed on click only.  Our song value
            // is maintained via a closure so it works.
            (
                // eslint-disable-next-line react/no-array-index-key
                <tr key={index} data-item={coord} onClick={this.onClickHandler}>
                    <td data-title="cc">{coord.lat},{coord.lon}</td>
                    <td data-title="ic" />
                    <td data-title="dlat" />
                    <td data-title="dlon" />
                </tr>
            )); // no need to bind with anon function
    }

    render() {
        console.log('Coords passed in from Accuracy Calculator :', this.props.coords);
       return (
            <div className="grid">
                <table id="table0">
                    {this.renderResultRows(this.props.coords)}
                </table>
            </div>
        );
    }
}

export default Grid;
3个回答

您需要将 e 放入您的函数中

在您的 onClickHandler 中,在参数中添加 e。

onClickHandler = (e) => {
   const song = e.target.getAttribute('data-item');
   console.log('We need to get the details for ', song);
    
}

我希望它能解决您的问题

ewef
2021-04-08

您没有将 onClickHandler 参数作为 e 传递, 请参阅以下代码。


import React from 'react';
import './gridStyle.css';

class Grid extends React.Component {

    onClickHandler = (e) => {
        const song = e.target.getAttribute('data-item');
        console.log('We need to get the details for ', song);
    }

    renderResultRows(data) {
        return data.map((coord, index) =>
            // anon func maintains scope!
            // Pass in a function to our onClick, and make it anon
            // to maintain scope.  The function body can be anything
            // which will be executed on click only.  Our song value
            // is maintained via a closure so it works.
            (
                // eslint-disable-next-line react/no-array-index-key
                <tr key={index} data-item={coord} onClick={this.onClickHandler}>
                    <td data-title="cc">{coord.lat},{coord.lon}</td>
                    <td data-title="ic" />
                    <td data-title="dlat" />
                    <td data-title="dlon" />
                </tr>
            )); // no need to bind with anon function
    }

    render() {
        console.log('Coords passed in from Accuracy Calculator :', this.props.coords);
       return (
            <div className="grid">
                <table id="table0">
                    {this.renderResultRows(this.props.coords)}
                </table>
            </div>
        );
    }
}

export default Grid;
sathish kumar
2019-03-01

您需要更改...

  onClickHandler = () => {
    const song = e.target.getAttribute('data-item');
    console.log('We need to get the details for ', song);
}

onClickHandler = (e) => {
    const song = e.target.getAttribute('data-item');
    console.log('We need to get the details for ', song);
}

以避免出现您遇到的错误。您可以在 此处 阅读有关事件对象的更多信息。

Tom Price
2019-03-01