代码之家  ›  专栏  ›  技术社区  ›  bp123

将元素附加到箭头函数

  •  1
  • bp123  · 技术社区  · 8 年前

    我使用箭头函数来创建一个按钮点击事件,我如何附加一个元素到函数。没有问题。

      handleButtonClick(classId) {
        this.setState({
          classId
        });
      }
    
      render() {
        return (
          <Button
            name="name"
            value={classId}
            onClick={e => this.handleButtonClick(e, classId)}
          >'Click me'
          </Button>
        );
      }
    
    3 回复  |  直到 8 年前
        1
  •  2
  •   pretzelhammer Paras Bhattrai    8 年前

    除了改变

    handleButtonClick(classId) {
    

    handleButtonClick(e, classId) {
    

    然后可以通过目标属性访问元素,即。 e.target

        2
  •  0
  •   Azamat Zorkanov    8 年前

    只是改变

    handleButtonClick(classId) {
    

    进入之内

    handleButtonClick(e, classId) {
    

    因为在onclick处理程序中,您首先传递e:

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

    希望这有帮助

        3
  •  0
  •   Adnan    8 年前

    只需在函数中添加事件参数,就可以使用event.target.value访问输入值

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