reactjs - React - Cannot read property 'state' of undefined -
i keep getting error despite using fat arrow function bind context of when using setstate. can please help?
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); })); }
from error message seems clear this
undefined. because using in search()
, search()
isn't bound component, making this
meaningless. fix try adding line @ end of constructor function:
this.search = this.search.bind(this);
now should able use this
in search function.
Comments
Post a Comment