javascript - 单击循环内的处理程序(两次返回)不起作用(React JS)

标签 javascript reactjs

我有一个非常简单的 React 片段,它遍历数组并输出选项卡名称。但是,我的点击处理程序不再有效(以前我没有循环时它有效)。

这一段与我之前使用 .map 循环的不同之处在于,这一段在渲染函数中有两个返回值。一个用于 React 需要的外部 div 元素,然后一个用于遍历对象。

有人知道我怎样才能成功让点击处理程序再次工作吗?

请看我的组件

class TabMenu extends React.Component {
constructor(props) {
    super(props);
    this.state = {

    };
    this.tabMenuList = [
      {
        title: 'My Account',
        section: 'MyAccount'
      },
      {
        title: 'Conference Details',
        section: 'MyAccount'
      },
      {
        title: 'My Abstract',
        section: 'MyAbstract'
      }
    ];
}
handleClick(e){
  e.preventDefault();

  console.log('this is the click handler', this);
  ReactDOM.render(<Conference />,document.getElementById('content'));
}
render() {

  return (
    <div>
      {this.tabMenuList.map(function(menuItem, index){
        return(
          <li data={menuItem.section}>
            <a onClick={this.handleClick.bind(this)} href={'#'}>
              <img src={'assets/img/mail_icon.jpg'} />
              <span>{menuItem.title}</span>
            </a>
          </li>
        )
      })}
    </div>
  );
}
}

最佳答案

解决方案

像这样使用 ES6 arrow 函数:

class TabMenu extends React.Component {
constructor(props) {
    super(props);
    this.state = {

    };
    this.tabMenuList = [
      {
        title: 'My Account',
        section: 'MyAccount'
      },
      {
        title: 'Conference Details',
        section: 'MyAccount'
      },
      {
        title: 'My Abstract',
        section: 'MyAbstract'
      }
    ];
}
handleClick(e){
  e.preventDefault();

  console.log('this is the click handler', this);
  ReactDOM.render(<Conference />,document.getElementById('content'));
}
render() {

  return (
    <div>
      {this.tabMenuList.map((menuItem, index) => {
        return(
          <li data={menuItem.section}>
            <a onClick={this.handleClick.bind(this)} href={'#'}>
              <img src={'assets/img/mail_icon.jpg'} />
              <span>{menuItem.title}</span>
            </a>
          </li>
        )
      })}
    </div>
  );
}
}

为什么?

在你的 React 代码中,this 没有引用 TabMenu

在函数中声明 this 时,它会自动默认为全局对象 - 在您的环境中为 Window

Since the following code is not in strict mode, and because the value of this is not set by the call, this will default to the global object.

然而,重要的是要知道这一点

In strict mode, however, the value of this remains at whatever it was set to when entering the execution context, so, in the following case, this will default to undefined.

为什么?因为根据this问题和第一个答案,ES6 模块 默认使用 strict,因此 function 中的 this 等于 undefined

因此,

In arrow functions, this is set lexically, i.e. it's set to the value of the enclosing execution context's this. In global code, it will be set to the global object

您的封闭执行上下文是 TabMenu

MDN 有一篇关于 this 的好文章,以及它如何根据调用 this 的上下文而变化。

https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Operators/this

关于javascript - 单击循环内的处理程序(两次返回)不起作用(React JS),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43047809/

相关文章:

JavaScript UTC 日期

javascript - 如果窗口调整大小时卸载 jquery 函数

javascript - PHP日期格式的倒计时

javascript - node.js req.body 在express 4.16.X、body-Parser 1.X 中未定义

javascript - 为什么我的 div 在较小的屏幕上可以拖动,但是当我在较大的屏幕上检查它时,我不再能够拖动它?

javascript - 使用 Javascript 更改隐藏字段的值

javascript - 为什么 this.state 在 render() 中不起作用

javascript - 在ReactJS中访问数组中的对象

javascript - 如何将来自 React 组件库的 Tailwind CSS 样式包含到应用程序 CSS 包中?

javascript - 'exhaustive-deps' 的警告不断要求提供完整的 'props' 对象,而不是允许单个 'props' 方法作为依赖项