私のレンダリング機能私は会社の名前を表示しようとしています。したがって、私はthis.companyに値company_nameを代入する関数getCompanyByShortlinkを呼び出します。私は応答をチェックし、必要なすべてのデータを含んでいるので、ここで問題はありません。返されたネイティブセットvarからフェッチ
これは機能しませんが、値は割り当てられません。私がreturn this.company = "test"を入力すると;直接、それは完全に正常に動作します。
私のAPIから得られる正しい価値を誰かに教えてもらえるかどうかは本当にありがたいです。
おかげで、オリバー
class Company extends React.Component {
constructor(props){
super(props);
this.shortlink = this.props.shortlink;
this.company = null;
}
getCompanyByShortlink(shortlink){
//this works perfectly fine!
// return this.company = "test";
fetch('http://192.168.178.96:81/api/v1/companies/'+shortlink).then((response) => response.json())
.then((responseJson) => {
//this does not work for any reason.
return this.company = responseJson.data.company.company_name;
})
.catch((error) => {
console.warn(error);
});
}
render() {
this.company = this.getCompanyByShortlink(this.shortlink);
return (
<View style={styles.mainContainer}>
<Text style={styles.mainWelcome}>Your Company: {this.company} </Text>
</View>
);
}
}。
'this.company = this.getCompanyByShortlink(this.shortlink);' 'promise'ない約束の解決された値にthis.companyを設定しています。 –