简体   繁体   中英

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

Just change

handleButtonClick(classId) {

into

handleButtonClick(e, classId) {

since in onClick handler, you first passing 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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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