简体   繁体   English

当我将函数传递给React.js的子组件时,为什么会出现未捕获的TypeError?

[英]Why am I getting an uncaught TypeError when I pass a function to a child component in React.js?

This is a small chunk of a larger codebase. 这只是较大代码库的一小部分。 But I believe this is the only relevant part needed for this question. 但我相信这是此问题所需的唯一相关部分。

I have two components DataSeries and Sector . 我有两个组件DataSeriesSector The DataSeries component is rendered by another parent component. DataSeries组件由另一个父组件呈现。 And the Sector component is rendered by the DataSeries component. Sector组件由DataSeries组件呈现。 I'm passing a function as a property from DataSeries to Sector . 我正在将一个函数作为属性从DataSeries传递给Sector But when I try to call that function from Sector I'm getting an Uncaught TypeError: this.props.someFunc is not a function error. 但是,当我尝试从Sector调用该函数时,出现了Uncaught TypeError: this.props.someFunc is not a function错误。 And I'm not really sure why. 我不太确定为什么。 Any help? 有什么帮助吗?

Here is the code. 这是代码。

var Sector = React.createClass({
  getInitialState: function() {
    return {text: '', count:'', opacity:'arc'};
  },
  formatNumber: function(num, digits) {
      //irrelevant function
      return num;
  },
  render: function() {
    var outerRadius = this.props.width/2.2;
    var innerRadius = this.props.width/3.5;
    var arc = d3.svg.arc()
        .outerRadius(outerRadius)
        .innerRadius(innerRadius);
    var data = this.props.data;
    var center = "translate(" + arc.centroid(data) + ")";
    var nameCenter = "translate(0,0)";
    var countCenter = "translate(0,50)"
    var color = this.props.colors;
    var formattedCount = this.formatNumber(this.state.count, 1)
    return (
      <g onMouseOver={this.onMouseOver} onMouseOut={this.onMouseOut} onClick={this.onClick}>
        <path className={this.state.opacity} fill={color[this.props.ikey]} d={arc(this.props.data)}></path>
        <text fill={color[this.props.ikey]} stroke={color} fontSize="27px" transform={nameCenter} textAnchor="middle">{this.state.text}</text>
        <text fill={"#868686"} stroke={color} fontSize="25px" transform={countCenter} textAnchor="middle">{formattedCount}</text>
      </g>
    );
  },

  onMouseOver: function() {

    this.setState({text: '', count:'', opacity:'arc-hover'});
    this.setState({text: this.props.name, count:this.props.data.value});
    console.log("Inside onMouseOver")
    this.props.someFunc();
  },
  onMouseOut: function() {
    this.setState({text: '', count: '', opacity:'arc'});
  },
  onClick: function() {
    //Do something on click?
  }
});

var DataSeries = React.createClass({
  propTypes: {
    width: React.PropTypes.number.isRequired,
    height: React.PropTypes.number.isRequired,
    color: React.PropTypes.array,
    data: React.PropTypes.array.isRequired,
  },
  randomFunction: function() {
    console.log("Random Function")
  },
  render: function() {
    var color = this.props.colors;
    var data = this.props.data;
    var width = this.props.width;
    var height = this.props.height;
    var pie = d3.layout.pie();
    var result = data.map(function(item){
      return item.count;
    })
    console.log(result)
    var names = data.map(function(item){
      return item.name;
    })
    var sum = result.reduce(function(memo, num){ return memo + num; }, 0);
    var position = "translate(" + (width)/2 + "," + (height)/2 + ")";
    var arcs = (pie(result)).map(function(point, i) {
      return (
        <Sector 
           data={point} 
           ikey={i} 
           key={i} 
           name={names[i]} 
           colors={color} 
           total={sum} 
           width={width} 
           height={height} 
           someFunc={this.randomFunction}/>
      )
    });
    return (
        <g transform={position}>
        {arcs}
        </g>
    );
  }
});

When you use map , the context represented by this changes. 当您使用map ,所代表的背景下this变化。 So, currently, inside your map call, this.randomFunction is actually "undefined" . 因此,当前,在您的map调用中, this.randomFunction实际上是"undefined"

So, to use the Sector 's context of this that has your function in it, you need to bind Sector 's this to the map 's anonymous function. 因此,使用Sector “的情景下this有你的功能,你需要绑定Sectorthismap的匿名函数。

var arcs = (pie(result)).map(function(point, i) {
  return (
    <Sector 
      data={point} 
      ikey={i} 
      key={i} 
      name={names[i]} 
      colors={color} 
      total={sum} 
      width={width} 
      height={height} 
      someFunc={this.randomFunction}
    />
  )
}.bind(this));

What this is doing is setting the context of this inside of the anonymous function to the this from Sector . 这是在将匿名函数内部的this的上下文设置为Sectorthis

暂无
暂无

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

相关问题 我为什么会收到“ Uncaught TypeError:下载不是函数” - Why am I getting “Uncaught TypeError: download is not a function” 为什么会出现“未捕获的TypeError:getEnumerator不是函数”? - Why am I getting, “Uncaught TypeError: getEnumerator is not a function”? Backbone.js 视图是否需要 jQuery 或 Zepto? (或者:为什么我会收到“未捕获的类型错误:未定义不是函数”?) - Do Backbone.js views require jQuery or Zepto? (Or: why am I getting “Uncaught TypeError: undefined is not a function”?) 为什么在另一个 function 中使用时会出现“未捕获的类型错误:regex.test 不是函数”? - Why am I getting 'Uncaught TypeError: regex.test is not a function' when used inside another function? 为什么在使用Flexslider时会出现“ Uncaught TypeError”? - Why am I getting “Uncaught TypeError” when using Flexslider? 为什么会出现“未捕获的TypeError”错误? - Why am I getting 'Uncaught TypeError' error? React.js:未被捕获的TypeError:undefined不是一个函数 - React.js : Uncaught TypeError: undefined is not a function 在 REACT.js 中,如何将状态从子组件传递到父组件作为道具 - In REACT.js how can I pass a state from child component up to Parent component as a prop 如何通过道具[REACT.JS]将图片网址从父组件传递到子组件 - How can I pass image url from parent component to child component via props [REACT.JS] 在React.js的父组件中使用react-router时,如何使用react context API将数据从父组件传递到子组件? - How do I use react context API to pass data from parent component to child component when using react-router in the parent component in React.js?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM