Javascript Chart js:react-chartjs-2中无法在甜甜圈图表的切片上添加onclick事件
我试图在一块油炸圈饼上添加onclick功能,它在console.log上运行良好,但在调用check函数时不起作用。它的支票未定义Javascript Chart js:react-chartjs-2中无法在甜甜圈图表的切片上添加onclick事件,javascript,reactjs,user-interface,chart.js,chart.js2,Javascript,Reactjs,User Interface,Chart.js,Chart.js2,我试图在一块油炸圈饼上添加onclick功能,它在console.log上运行良好,但在调用check函数时不起作用。它的支票未定义 class DoughnutChart extends React.Component { constructor(props) { super(props); this.state = {}; } check = (x, b) => { console.log("test 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;
class DoughnutChart扩展了React.Component{
建造师(道具){
超级(道具);
this.state={};
}
检查=(x,b)=>{
控制台日志(“测试检查”);
};
render(){
返回(
);
}
}
导出默认甜甜圈图;
您无权访问函数中的“this”,因此可以绑定该函数或使用如下箭头函数
onClick: (evt, item) =>{
this.check("source", this.state.data.datasets[0].data[item[0]._index]);
console.log("test");
},
您还必须调用this.check并使用this.state.data