React:将值从onClick传递给方法

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了React:将值从onClick传递给方法相关的知识,希望对你有一定的参考价值。

How to pass a parameter into a method from an onClick method found in the render method.

Majority of the times when you need to do this, your adding it through a loop. In this case, call a function where the item in the loop will automatically be passed into the function. This will prevent the component from re-rendering every-time it's parent changes and none of it's props or state changed.

** DO NOT DO **
Adding the code below marked with DON'T DO, will cause this component to re-render every-time it's parent changes, regardless of whether this components state or props changed.
  1. renderUser = user => {
  2. return <User key={user.id} user={user} onClick={this.deleteUser} />;
  3. }
  4.  
  5. render() {
  6. return (
  7. <div>
  8. <h1>Users</h1>
  9. <ul>
  10. {this.state.users.map(this.renderUser)}
  11. </ul>
  12. </div>
  13. );
  14. }
  15.  
  16.  
  17. // DON'T DO
  18. return (
  19. <th value={column} onClick={() => this.handleSort(column)}>{column}</th>
  20. );

以上是关于React:将值从onClick传递给方法的主要内容,如果未能解决你的问题,请参考以下文章

如何将值从片段/活动传递到 xml?

React js onClick无法将值传递给方法

React js onClick无法将值传递给方法

将值从回收器适配器传递到android中的片段

如何将值从一个片段的回收器视图项传递到另一个片段

将值从灯箱传递给父级