handle在映射数组时进行反应

时间:2017-08-24 03:57:11

标签: javascript reactjs

我正在尝试通过数组映射并创建将更新状态的链接。当我通过数组映射并创建链接时,我得到:

TypeError:无法读取属性' handleClick'未定义的



var FilterList = React.createClass({
  handleClick : function(e, list){
          this.props.callbackFromParent(list);
                this.setState({showing : list});
            },
  render: function() {
          if(this.props.sdata ){
           var seasons ='';
            seasons = this.props.sdata.Seasons.map(function(season, i) {
              if(i < 30) {
              var playkey = season.mediaId;
              var name = season.Season;
          return (
             <li> <a href="#" onClick={(e) => this.handleClick(e, {playkey})}>{name}</a></li>
          );
        }else{
          return (<div key={playkey}></div>);
        }
      });
         return (
      <Row className="singleChannelSeasonsList">
        <Grid>
            <Col md={12}>
                <ul className="seasons">
                {seasons}
                </ul>
            </Col>
      </Grid>
    </Row>
    );
          } else {
    return (

      <Row className="singleChannelSeasonsList">
        <Grid>
            <Col md={12}>
                <ul className="seasons">
                <li><a href="#" className="activeItem" onClick={(e) => this.handleClick(e, "list")}>All</a></li>
                    <li> <a href="#" onClick={(e) => this.handleClick(e, "category/Entertainment")}>Entertainment</a></li>
                    <li><a href="#" onClick={(e) => this.handleClick(e, "category/Health%20&%20Wellness")}>Health &amp; Wellness</a></li>
                    <li> <a href="#" onClick={(e) => this.handleClick(e, "category/Opinion")}>Opinion</a></li>
                </ul>
            </Col>
      </Grid>
    </Row>
    );
   }
  }
});
export default FilterList;
&#13;
&#13;
&#13;

如何映射数据并生成手动链接。 {this}是绑定的,当我没有this.props.sdata时它会起作用。谢谢!

3 个答案:

答案 0 :(得分:1)

问题是“this”关键字的值取决于函数的调用方式。 “this”在“map()”中的匿名函数内部调用。所以“这个”将是未定义的。

解决方案:

  1. 将“this”绑定到匿名函数
  2.   this.props.sdata.Seasons.map(function(season, i) {
        //....blah blah blah      
      }.bind(this));

    1. 将“this”设置为“map()”函数
    2. 的第二个参数

        this.props.sdata.Seasons.map(function(season, i) {
             //....blah blah blah 
        },this);

答案 1 :(得分:0)

您尚未将{this}绑定到<?php ini_set("xdebug.var_display_max_children", -1); ini_set("xdebug.var_display_max_data", -1); ini_set("xdebug.var_display_max_depth", -1); ob_start(); include_once $_SERVER['DOCUMENT_ROOT'] . '/CommandWks/phpAssets/Classes/PHPExcel.php'; $exceldata = array(); //$inputfilename = $_POST['label']; $inputfilename = basename($_FILES['fileName']); var_dump($inputfilename); $inputfiletype = PHPExcel_IOFactory::identify($inputfilename); $objReader = PHPExcel_IOFactory::createReader($inputfiletype); $objPHPExcel = $objReader->load($inputfilename); $excelReader = PHPExcel_IOFactory::createReaderForFile($inputfiletype); $excelObj = $excelReader->load($inputfiletype); $worksheet = $excelObj->getSheet(0); $lastRow = $worksheet->getHighestRow(); var_dump($lastRow); ob_end_flush(); ?>

应该是:

this.props.sdata.Seasons.map( () => {} )

答案 2 :(得分:0)

如果我理解你的问题,你需要将它绑定到你的事件处理程序:

...
onClick={this.handleClick.bind(this)}
...