【发布时间】:2017-08-15 08:56:59
【问题描述】:
尽管在使用 setState 时使用胖箭头函数绑定 this 的上下文,但我仍然收到此错误。有人可以帮忙吗?
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);
}));
}
【问题讨论】:
-
你能看到发生错误的代码行吗?
-
fetchUser 失败。我只是添加了这个变量。常量查询 = this.state.query;谢谢修复!
-
添加到我的答案中,您应该使用箭头函数,因为它们将重用父
this
标签: reactjs