简体   繁体   English

反应OnClick功能不绑定

[英]React OnClick Function not Binding

I have an onClick handler that is bound to currentAreas component. 我有一个绑定到currentAreas组件的onClick处理程序。 The onClick handler is called simultaneously when the currentAreas component is called, but does not work after that. 当调用currentAreas组件时,会同时调用onClick处理程序,但此后将无法使用。

onClick does not work when I try to click the anchor tag and I think it might be due to the way I'm binding the onClick function. 当我尝试单击锚标记时, onClick不起作用,我认为这可能是由于我绑定onClick函数的方式所致。

currentAreas.js currentAreas.js

import React, { Component, PropTypes } from 'react';

export default class currentAreas extends Component {
  constructor(props) {
    super(props);
  }

  render() {
    const { onClick } = this.props;

    return(
        <div className="list-group panel">
          <a href="#demo3" className="list-group-item" onClick={onClick("All", "All")} >All</a>

        </div>
    );
  }
}

currentList.js (Main component) currentList.js(主要组件)

class currentList extends Component {
    constructor(props) {
      super(props);

      this.updateLocation = this.updateLocation.bind(this);
    }

  updateLocation(name, nameLocation){
    console.log(name);
    console.log(nameLocation);
  }

    render() {
        return (
           <div className="step-3-container">

            <CurrentAreas 
              onClick ={this.updateLocation} />
        </div>
      )
    }
  }

Whenever your javascript interpreter "sees" a "()" it tries to execute the function that is before "()". 每当您的JavaScript解释器“看到”“()”时,它都会尝试执行“()”之前的函数。 Therefore, what your code is actually doing is executing onClick (the one that came from props ), passing as arguments "All" and "All". 因此,您的代码实际上在执行 onClick (来自props ),并将其作为参数“ All”和“ All”传递。 So, what you need to do is, instead of calling it yourself, let the onClick handler call it. 因此,您需要做的是让onClick处理程序调用它,而不是自己调用它。

In other words, the onClick prop must receive a function . 换句话说, onClick道具必须接收一个function

One possible solution is to wrap your onClick into an arrow function, doing something like that: 一种可能的解决方案是将onClick包装到箭头函数中,执行以下操作:

<a href="#demo3" onClick={() => onClick("All", "All")}>All</a>

Or, you may bind the parameters to your function (bind returns a function, so it will fit well on onClick ). 或者,您可以bind参数bind到函数(bind返回一个函数,因此非常适合onClick )。

<a href="#demo3" onClick={onClick.bind(null, "All", "All")}>All</a>

The first bind parameter is the this value inside the binded function. 第一个bind参数是绑定函数内部的this值。

FMI about bind : https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_objects/Function/bind 关于bind FMI: https : //developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_objects/Function/bind

Youre executing the function during the declaration of the component, which likely means youre triggering it on render, not click 您在组件声明期间执行该函数,这可能意味着您在渲染时触发了该函数,而不是单击

onClick={onClick("All", "All")}

needs to be 需要是

onClick={onClick.bind(null, "All", "All")}

or 要么

onClick={function(){ onClick("All", "All"); }}

The onClick of a react component needs a callback that can be executed. react组件的onClick需要可以执行的回调。 you are passing in the return value of this.props.onClick because youre calling it right away with arguments 您正在传递this.props.onClick的返回值,因为您使用参数立即调用了它

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM