[英]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
. 我有两个组件
DataSeries
和Sector
。 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
有你的功能,你需要绑定Sector
的this
对map
的匿名函数。
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
的上下文设置为Sector
的this
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.