简体   繁体   English

我可以向函数发送事件和参数吗?

[英]Can I send event and a param to a function?

Here is my code : 这是我的代码:

export default class HeroDetail extends Component {
constructor(props) {
   super(props);
 }

 updateHeroName(event) {
     this.props.actions.changeName(this.props.heroAppState.selectedHero.id, event.target.value);
 }
 render() {
   const _hero = this.props.heroAppState.selectedHero;
     return (
           <div>
             <input placeholder="name"
               value={_hero.name}
               onChange={this.updateHeroName.bind(this)} />
            </div>             
     );
   }
 } 

I want to bind updateHeroName at the top. 我想在顶部绑定updateHeroName And pass 2 param(hero_id and event) into updateHeroName But I don't know how to pass event and a param named _hero.id , So the code not work. 并将2个param(hero_id和event)传递到updateHeroName但是我不知道如何传递event和一个名为_hero.id的参数,因此代码无法正常工作。 Can I do this in javascript?? 我可以用javascript吗?

// Fail code 
export default class HeroDetail extends Component {
constructor(props) {
   super(props);
   this.updateHeroName = this.updateHeroName.bind(this);
 }

 updateHeroName(hero_id, event) {
     this.props.actions.changeName(hero_id, event.target.value);
 }
 render() {
   const _hero = this.props.heroAppState.selectedHero;
     return (
           <div>
             <input placeholder="name"
               value={_hero.name}
               onChange={this.updateHeroName(_hero.id, event)} />
            </div>             
     );
   }
 } 

This should work: 这应该工作:

export default class HeroDetail extends Component {
constructor(props) {
   super(props);
   this.updateHeroName = this.updateHeroName.bind(this);
 }

 updateHeroName(hero_id, event) {
     this.props.actions.changeName(hero_id, event.target.value);
 }
 render() {
   const _hero = this.props.heroAppState.selectedHero;
     return (
           <div>
             <input placeholder="name"
               value={_hero.name}
               onChange={(event) => this.updateHeroName(_hero.id, event)} />
            </div>             
     );
   }
 } 

You can do like onChange={ (event) => this.updateHeroName(‌​_hero.id, event) } (as Alexander T suggested) 您可以像onChange={ (event) => this.updateHeroName(‌​_hero.id, event) } (就像Alexander T建议的那样)

But generally, it's better not to use arrow functions or bind methods inside render as it generates a new copy of the function on any render call. 但是通常,最好不要在render内部使用箭头函数或bind方法,因为它会在任何render调用上生成该函数的新副本。 Move function declaration to the class constructor . 将函数声明移至class constructor

I personally prefer to use arrow functions as class properties in this case 我个人更喜欢在这种情况下使用箭头函数作为类属性

class MyClass extends React.Component {

  handleClick = () => {
    // your logic
  };

  render() {
    return (
      <button onClick={this.handleClick}>Click me</button>
    );
  }
}

It's not a part of ES2015 specification but babel stage-0 preset supports this syntax 它不是ES2015规范的一部分,但babel stage-0预设支持此语法

You can read more about context binding in React in this article 您可以在本文中的React中阅读有关上下文绑定的更多信息

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

相关问题 JavaScript 我无法将字符串参数发送到 function 即使我可以在单击事件上使用 div.innerHTML 发送 int 参数 - JavaScript I can't send string parameter to function even I can send int parameter with div.innerHTML on click event 当我在 on 事件期间将数据发送到函数时,jquery 中的 on 事件不起作用,这是正常的,那么我该如何解决? - Is this noramal that on event in jquery does not work out when I send data to a function during on event, then how can I fix that? 如何确保函数/方法参数是certian类型? - How can I ensure a function/method param is of a certian type? 如何使用带有 1 个参数的 jquery function 来 append 图像? - How can I append an Image using a jquery function with 1 param? 动态添加事件侦听器。 函数触发,但如何发送变量参数? - Dynamically adding event listener. Function fires but how can I send a variable parameter? 当前元素作为其Event函数参数 - The current element as its Event function param 如何在事件不是参数的函数中添加preventDefault - How to add preventDefault in a function where event is not the param Javascript-将参数函数传递给异步onchange事件 - Javascript - Pass param function to async onchange event 反应在事件之外的函数中使用参数 - React Use param in function outside of event 通过javascript将事件发送给函数 - send an event to a function by javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM