太字のArrow関数を使用してもこのエラーが発生しますが、setStateを使用する場合はこのコンテキストをバインドします。誰でも助けてくれますか?React - 未定義のプロパティ 'state'を読み取ることができません
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; ありがとうございました! –
私の答えには、親が 'this'を再利用するため、矢印関数を使うべきです。 – webdeb