【发布时间】:2019-09-26 15:22:40
【问题描述】:
我正在尝试使用从 axios get 返回的数据设置状态。但我不断收到此错误 TypeError: Cannot read property 'setState' of undefined。
类页面扩展 React.Component {
constructor(props) {
super(props);
this.authenticated = props.authenticated;
//this.handleClick = this.handleClick.bind(this);
}
state ={
page : []
}
componentDidMount() {
let config = null;
//let token = null;
app.auth().currentUser.getIdToken(true).then(function(idToken) {
config = {
headers: {'Authorization': idToken}
};
console.log(config);
axios.get('http://localhost:4001/api/v1/page',config)
.then(res => {
console.log(res.data);
const page = res.data;
this.setState( page );
}).catch((err)=>{
console.log(err);
});
})
}
render () {
const authenticated = this.authenticated;
console.log(authenticated);
console.log(this.state);
return (
<h1> test 123</h1>
);
}
}
【问题讨论】: