Reactjs 如何设置状态,继续获取TypeError:无法读取未定义的属性“setState”
我正试图用从axios get返回的数据设置状态。但我一直收到以下错误类型错误:无法读取undefined的属性“setState” 类页扩展了React.Component{Reactjs 如何设置状态,继续获取TypeError:无法读取未定义的属性“setState”,reactjs,setstate,Reactjs,Setstate,我正试图用从axios get返回的数据设置状态。但我一直收到以下错误类型错误:无法读取undefined的属性“setState” 类页扩展了React.Component{ constructor(props) { super(props); this.authenticated = props.authenticated; //this.handleClick = this.handleClick.bind(this); } state ={ page
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>
);
}
}axios内部指的是不同的东西,将其值存储在变量中并使用该变量
componentDidMount = () => {
let config = null;
//let token = null;
const current = this; // here save this in current variable
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;
current.setState(page); // use current here
})
.catch(err => {
console.log(err);
});
});
}
谢谢你,伙计,你是一个传奇。希望你圣诞节得到你想要的一切