javascript - how to call multiple methods in onClick in react? - Stack Overflow

I have two ponents (Parent ponent & Child ponent) in my react app. I have two button clicks in my c

I have two ponents (Parent ponent & Child ponent) in my react app. I have two button clicks in my child ponent and I need to pass two props to the parent ponent. I use the code as follows. The problem is, I can't include both methods in the parent ponent's element, but I need to. How can I use both edituser and deleteuser functions in the parent ponent?

Child ponent:

class EnhancedTable extends React.Component {
  constructor(props){
    super(props);
    this.state = {
      userID: 10
    };
    this.editByUserId = this.sendUserId.bind(this);
    this.DeleteByUserId = this.sendUserId.bind(this);
  }

  editByUserId() {
    this.props.onClick(this.state.userID);
  }

  DeleteByUserId() {
    this.props.onClick(this.state.userID);
  }

  render() {
    return (
       <button onClick={this.sendUserId}>
           <BorderColorIcon onClick={this.editUserById} className="action margin-r" />
           <DeleteIcon onClick={this.deleteUserById} className="action margin-r" />
       </button>
    )
  }
}

Parent ponent:

Import EnhancedTable from './EnhancedTable';

class Users extends Component {

  constructor(props) {
    super(props);
    this.state = {
      userID: null
    };
    this.editUser = this.editUser.bind(this);
    this.deleteUser = this.deleteUser.bind(this);
   }

  editUser(idd) {
    this.setState({
      userID : idd
    })
    console.log("User Edited");
  }

   deleteUser(idd) {
      this.setState({
        userID : idd
      })
      console.log("User Deleted");
    }

  render() {
     return(
         <EnhancedTable onClick = {(e)=>{this.editUser; this.deleteUser;}}/>
     )
   }
}

I have two ponents (Parent ponent & Child ponent) in my react app. I have two button clicks in my child ponent and I need to pass two props to the parent ponent. I use the code as follows. The problem is, I can't include both methods in the parent ponent's element, but I need to. How can I use both edituser and deleteuser functions in the parent ponent?

Child ponent:

class EnhancedTable extends React.Component {
  constructor(props){
    super(props);
    this.state = {
      userID: 10
    };
    this.editByUserId = this.sendUserId.bind(this);
    this.DeleteByUserId = this.sendUserId.bind(this);
  }

  editByUserId() {
    this.props.onClick(this.state.userID);
  }

  DeleteByUserId() {
    this.props.onClick(this.state.userID);
  }

  render() {
    return (
       <button onClick={this.sendUserId}>
           <BorderColorIcon onClick={this.editUserById} className="action margin-r" />
           <DeleteIcon onClick={this.deleteUserById} className="action margin-r" />
       </button>
    )
  }
}

Parent ponent:

Import EnhancedTable from './EnhancedTable';

class Users extends Component {

  constructor(props) {
    super(props);
    this.state = {
      userID: null
    };
    this.editUser = this.editUser.bind(this);
    this.deleteUser = this.deleteUser.bind(this);
   }

  editUser(idd) {
    this.setState({
      userID : idd
    })
    console.log("User Edited");
  }

   deleteUser(idd) {
      this.setState({
        userID : idd
      })
      console.log("User Deleted");
    }

  render() {
     return(
         <EnhancedTable onClick = {(e)=>{this.editUser; this.deleteUser;}}/>
     )
   }
}
Share Improve this question edited Mar 25, 2019 at 14:20 David Johns asked Mar 25, 2019 at 14:05 David JohnsDavid Johns 1,7646 gold badges23 silver badges60 bronze badges 2
  • 2 You may create another function and move all the functions to be called in it and reference it in JSX onClick attribute. – Mohammad Usman Commented Mar 25, 2019 at 14:06
  • I did that and then console says that idd is not defined – David Johns Commented Mar 25, 2019 at 14:11
Add a ment  | 

2 Answers 2

Reset to default 5

You missed your ()

<EnhancedTable onClick = {(e)=>{this.editUser(); this.deleteUser();}}/>

You are doing it right in

 <EnhancedTable onClick = {(e)=>{this.editUser; this.deleteUser;}}/>

A minor change is needed:

<EnhancedTable onClick = {(e)=>{this.editUser(e); this.deleteUser(e);}}/>

A quick reference for what changed here:

let x = () => {
 console.log('hello');
}

x; // This simply does nothing as it is just a reference to the function

x(); // This instead invokes the function

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745437724a4627686.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信