chart.js - 我无法更新我的图表(在 react-chartjs-2 中)
问题描述
我想每秒钟更新一次图表
这是我的代码:
class Chart extends Component {
constructor(props) {
super(props);
this.state = {
chartData: {
// my charData
}
}
setInterval(function(){
this.props.changeHandler();
}, 500);
}
changeHandler() {
this.state.update();
}
// my render
}
但我有这个错误:this.props is undefined (On the setInterval)
任何人都可以帮助我吗?
解决方案
函数有自己的 this 上下文,因此您必须在 setInterval 之前将 this 上下文存储在局部变量中,或者使用箭头函数而不是函数
setInterval(() => {
this.props.changeHandler();
}, 500);
推荐阅读
- r - UseMethod("html_table") 中的错误:没有适用于 'html_table' 的方法应用于“xml_missing”类的对象 >
- forms - 是否可以在 ASP.Net 中扩展和继承 WebApplication?
- docker - 我可以在没有管理员权限的情况下在 Windows 上运行 Docker Desktop 吗?
- haskell - Haskell 高阶函数类型
- android - 无法同时使用 Firebase 和 Mapbox 构建项目
- node.js - 带有 nodejs /express 和 mongodb 的实时 CRUD 应用程序
- git - 有没有办法根据文件扩展名配置 git 以使用不同的 difftool?
- python - 通过文本单词列表或 csv 批量检查我的 s3 存储桶权限
- javascript - 如何避免在每个测试文件中导入组件所需的所有内容?
- graphql - Apollo 缓存正在重置为旧数据