简体   繁体   English

在React的事件处理程序onClick()中,将prop值作为参数传递给箭头函数

[英]Pass prop value as a parameter in an arrow function in an event handler onClick() in React

I'm trying to pass a value of prop in a function which is invoked on onClick(), but I'm getting the following error when I try to console.log() that value inside the function. 我正在尝试在onClick()上调用的函数中传递prop的值,但是当我尝试在该函数内console.log()该值时出现以下错误。

Error: 错误:

**Warning: This synthetic event is reused for performance reasons. **警告:出于性能原因,此综合事件被重用。 If you're seeing this, you're accessing the property nativeEvent on a released/nullified synthetic event. 如果看到此消息,那么您正在访问已发布/无效的合成事件的nativeEvent属性。 This is set to null. 设置为空。 If you must keep the original synthetic event around, use event.persist(). 如果必须保留原始的合成事件,请使用event.persist()。

class Calculator extends React.Component {
  constructor(props) {
    super(props);
    this.evaluateInfixString = this.evaluateInfixString.bind(this);
    this.appendInfixString = this.appendInfixString.bind(this);
    this.state = {
      buttons: ["+", "-", "*", "/", 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, "clear", "="],
      infixString: null
    };
  }
  evaluateInfixString() {
    console.log("perform operation");
  }
  appendInfixString(buttonPressed) {
    console.log(buttonPressed);
  }
  render() {
    return (
      <div id={"calculator"}>
        <Display value={this.state.infixString} />
        <Buttons
          buttons={this.state.buttons}
          appendInfixString={this.appendInfixString}
        />
      </div>
    );
  }
}

class Display extends React.Component {
  render() {
    return <div id={"display"}>{this.props.value}</div>;
  }
}

class Buttons extends React.Component {
  render() {
    return (
      <div id={"buttons"}>
        {this.props.buttons.map(button => {
          return <button className={"button"} onClick={(button) => this.props.appendInfixString(button)}>{button}</button>;
        })}
      </div>
    );
  }
}

const rootElement = document.getElementById("root");
ReactDOM.render(<Calculator />, rootElement);

Change: 更改:

return <button className={"button"} onClick={(button) =>  this.props.appendInfixString(button)}>{button}</button>;

To: 至:

    return <button className={"button"} onClick={() => this.props.appendInfixString(button)}>{button}</button>;

Here's a working codepen . 这是一个工作中的codepen If you open the console, you'll see that the number or character is logged to the console for you. 如果您打开控制台,您会看到数字或字符已为您登录到控制台。

Another way to accomplish what you'd like to do would be something like what I've done in this codepen . 完成您想要做的事情的另一种方法是,就像我在本Codepen中所做的一样 You'll be passing the event back to the parent and then you can access the value with e.target.value like I've shown in your parent component. 您将把事件传递回父级,然后可以像我在父级组件中显示的那样使用e.target.value访问该值。 In that case, you're child component would have a click handler like this: 在这种情况下,您的子组件将具有如下单击处理程序:

<button type="button" className={"button"} value={button} onClick={this.props.appendInfixString}>{button}</button>

The button value will be passed back with the event in the parent click event handler, which you can access there. button值将与父单击事件处理程序中的event一起传递回去,您可以在那里访问该button

What is passed to onClick is click event, so in your code below 传递给onClick是点击事件,因此在下面的代码中

<button className={"button"} onClick={(button) =>this.props.appendInfixString(button)}>{button}</button>

what you pass to the appendInfixString is not the button string that the user clicks, but rather the click event. 您传递给appendInfixString的不是用户单击的按钮字符串,而是单击事件。 If you need to pass the button string being clicked, try 如果您需要传递被点击的按钮字符串,请尝试

<button className={"button"} onClick={() => this.props.appendInfixString(button)}>{button}</button>

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

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