我试图在甜甜圈的一片上添加onclick功能,它在console.log上工作得很好,但如果调用check函数就不起作用了。它的给予检查是未定义的。
class DoughnutChart extends React.Component {
constructor(props) {
super(props);
this.state = {};
}
check = (x, b) => {
console.log("test check");
};
render() {
return (
<div>
<Doughnut
data={data}
height={1}
width={1}
options={{
responsive: true,
maintainAspectRatio: true,
cutoutPercentage: 85,
tooltips: false,
onClick: function (evt, item) {
check("source", data.datasets[0].data[item[0]._index]);
console.log("test");
},
}}
/>
</div>
);
}
}
export default DoughnutChart;发布于 2020-11-06 02:30:31
你不能在函数中访问' this‘,所以你可以绑定它或者使用箭头函数,如下所示
onClick: (evt, item) =>{
this.check("source", this.state.data.datasets[0].data[item[0]._index]);
console.log("test");
},此外,您还必须调用this.check并使用this.state.data
https://stackoverflow.com/questions/64702336
复制相似问题