React.jsで、superagentを利用して、Ajax通信を行いその後、stateにデータを保存しようとしております。
通信後、this.setStateにデータを保存しようとすると、
Uncaught TypeError: Cannot read property 'setState' of undefined
と表示され、setStateがないとエラー表示されます。
スコープが変わっているため、「this」が指し示しているものが想定しているものではないのは理解しておりますが、superagentにどうやって、thisを渡してあげるのか、わかっておりません。
皆様、ご教授いただけないでしょうか?
宜しくお願い致します。
export default class ShotData extends React.Component { constructor(props: Props) { super(props); this.state = {data: []} this.handleClick = this.handleClick.bind(this); } componentDidMount() { this.setState({data: [{id: "1"}]}); var uri = "http://localhost:8080/common/equipments" request.get(uri) .end(cb) function cb(err, res) { if (err) throw err; this.setState({data: res.body}); } } handleClick(e) { e.preventDefault(); } render() { return ( <div> ...</div> ) } }
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。