我有一个像这样的简单组件:
import React, {
Component
} from 'react';
import {
StyleSheet,
Text,
View
} from 'react-native';
export default class Statistics extends Component {
constructor(props) {
super(props);
this.state = {
data: []
}
}
componentDidMount() {
}
render() {
var config = {
chart: {
events: {
load: function() {
console.log(this.state.data);
}
}
}
};
return (null);
}
}
如何在加载函数内的状态下访问数据数组?
我不想绑定load().bind(this)
函数。
感谢您的考虑。
这个问题有所不同,因为我们正在处理一个我们不得不使用这个关键字的范围。请查看我上面更新的问题。
答案 0 :(得分:0)
一种方法:在渲染功能的第一行,您可以保存对state
的引用。
const state = this.state;
然后使用常量state
来引用组件状态。