简体   繁体   English

映射时将props参数传递给React.js中的函数

[英]Pass props parameter to function in React.js when mapping

When I am mapping through props in a components render method, can I pass in prop values to a function. 当我在组件render方法中通过prop映射时,可以将prop值传递给函数。 I have forgotten the correct way to do this. 我忘记了正确的方法。 For instance I have a category tag control which just lists some categories with a name, however onClick I want to perform some functionality for the individual category. 例如,我有一个类别标签控件,该控件仅列出了一些具有名称的类别,但是onClick我想为单个类别执行一些功能。

var React = require("react");

var CategoryTags = React.createClass({

  propTypes: {
    categories: React.PropTypes.array.isRequired
  },

  render: function() {

      var categoryRows = this.props.categories.map(function (c, i) {

        return (
            <button onClick={ this.linkToCategory.bind(name) } >
              {c.name}
            </button>
        );

      }.bind(this));

    return (
        <span>{categoryRows}</span>
    );

  },

  linkToCategory: function (c) {
    console.log(c);
  }

});

So in this example, while mapping though the categories, I want to pass in the name of the individual category so I can parse the link. 因此,在此示例中,在通过类别进行映射时,我想传递各个类别的名称,以便我可以解析链接。 Of course it would make sense for the object to have a link property but it didn't in this case. 当然,对象具有链接属性是有意义的,但在这种情况下则没有。

Example of the categories object being passed in to the component props 将类别对象传递给组件prop的示例

categories = [{'name': 'cat1'}, {'name': 'cat2'}];

Binding a function inside .map is not a good idea, because you are always going to pass a new function to the component, even if no other prop changed. .map内绑定一个函数不是一个好主意,因为即使没有其他属性更改,您也总是要将新函数传递给组件。 That means it will always be re-rendered, even if it doesn't have to be. 这意味着它将始终被重新渲染,即使不必如此。

But anyways, the problem with your code is that 但是无论如何,代码的问题是

  1. There is no variable name . 没有变量name
  2. You are using .bind incorrectly. 您使用的.bind错误。

The call should be 电话应该是

this.linkToCategory.bind(this, c.name)

The first argument passed to bind .bind is the value of this , not the first argument (which you should know since you are using .bind(this) as well). 传递给bind .bind的第一个参数是this的值,而不是第一个参数(由于您也使用.bind(this) ,因此应该知道.bind(this) )。

You can learn more about .bind in the MDN documentation . 您可以在MDN文档中了解有关.bind更多信息。

You can also return a function from your event handler: 您还可以从事件处理程序中返回一个函数:

var CategoryTags = React.createClass({

  propTypes: {
    categories: React.PropTypes.array.isRequired
  },

  render: function() {

      var categoryRows = this.props.categories.map(function (c, i) {

        return (
            <button onClick={this.linkToCategory(c)} >
              {c.name}
            </button>
        );

      }.bind(this));

    return (
        <span>{categoryRows}</span>
    );

  },

  linkToCategory: function (c) {
      return () => {
          console.log(c);
      }
  }

});

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

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