简体   繁体   English

将元素附加到箭头函数

[英]Attach element to arrow function

I am using arrow functions to create an event on a button click, how do I attach an element to the function.我正在使用箭头函数在单击按钮时创建一个事件,如何将元素附加到该函数。 Noob question.菜鸟问题。

  handleButtonClick(classId) {
    this.setState({
      classId
    });
  }

  render() {
    return (
      <Button
        name="name"
        value={classId}
        onClick={e => this.handleButtonClick(e, classId)}
      >'Click me'
      </Button>
    );
  }

Aside from changing除了改变

handleButtonClick(classId) {

to

handleButtonClick(e, classId) {

You can then access the element via the target property, ie e.target然后您可以通过 target 属性访问该元素,即e.target

Just change只是改变

handleButtonClick(classId) {

into进入

handleButtonClick(e, classId) {

since in onClick handler, you first passing e:因为在 onClick 处理程序中,您首先传递 e:

onClick={e => this.handleButtonClick(e, classId)}

Hope this helps希望这可以帮助

You just have to add event argument in your function and you can access the input value by using event.target.value你只需要在你的函数中添加事件参数,你就可以使用 event.target.value 访问输入值

    handleButtonClick(event,classId) {
     this.setState({
     classId:event.target.value
     });
    }

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

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