我正在尝试使用来自axios get的数据返回来设置状态,但是我一直收到此错误TypeError:无法读取未定义的属性'setState'。
类页面扩展了React.Component {
@echo off
set "ATTR=%AppData%\Microsoft\Excel\XLSTART"
For %%Z In ("%ATTR%") Do If "%%~aZ" GEq "d" GoTo DIR
Else If "%%~aZ" GEq "-" GoTo FILE
Else GoTo NOTFOUND
:DIR
Echo "Dir Found!"
Pause
:FILE
Echo "File Found!"
Pause
:NOTFOUND
Echo "NOTHING FOUND!"
Pause
}
答案 0 :(得分:1)
this 指的是另一件事,将this
的值存储在变量中并使用该变量
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);
});
});
}