尽管使用了胖箭头函数,但在使用setState时绑定了此上下文,我仍然不断收到此错误。任何人都可以帮忙吗?反应 - 无法读取未定义的属性'状态'
export default class App extends Component {
constructor(props) {
super(props);
this.state = {
query: '',
items: [],
repos: []
};
}
search(term) {
this.setState({
query: term
});
const clientId = '12489b7d9ed4251ebbca';
const secret = 'ff53ac9a93aaa9e7cddc0c009d6f068302866ff2';
function fetchUser() {
return axios.get(`https://api.github.com/users/${this.state.query}/repos?client_id=${clientId}client_secret=${secret}`);
}
function fetchRepos() {
return axios.get(`https://api.github.com/users/${this.state.query}?client_id=${clientId}client_secret=${secret}`);
}
axios.all([fetchUser(), fetchRepos()])
.then(axios.spread((items, repos) => {
this.setState({
items: items.data,
repos: repos.data
});
console.log(state);
}));
}
您是否能够看到发生错误的代码行? – webdeb
它在fetchUser上失败。我只是添加这个变量。 const query = this.state.query; 谢谢修复它! –
添加到我的答案,你应该使用箭头函数,因为他们会重用父'这个' – webdeb