import Request from 'superagent';
const getApi =() => {
let url = '/* URL */';
return Request.get(url).then((res) => {
this.setState({
content: res.body
});
});
}
export default getApi;
の外部ファイルにfunction
を作成しました。 function/setState
への外部ファイルへのアクセス方法を教えてください。外部関数でsetStateを使用するには?
componentWillMount(){
getApi();
}
私はこのエラーが表示されます。
'TypeError: Cannot read property 'setState' of undefined'
[どのように応答を返さないのが重複する可能性非同期呼び出しから?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) –
'getApi()'メソッドに 'コールバックメソッド'を渡し、レスポンスを取得したらそのメソッドを呼び出し、そのコールバックメソッド内でsetStateを実行します。 –
あなたは応答を返してから関数にsetStateを返す必要があります –