特定の時点でタイトル、説明、ユーザー情報の情報フィードを取得する予定のアプリを作成しています。私はこれを反応ネイティブでやっています。これらは同じビュー内の2つの別々のフィードであり、2つのタブを切り替えることができるので、私は現在のアーキテクチャーがその後最適であると考えました。 Methods.jsからコンポーネントのライフサイクルでの問題/未定義の処理ができません
:
当面の問題は
exports.getFriendFeed = function(TOKEN) {
return new Promise((resolve, reject) => {
fetch(baseUrl + 'feed' , {
method: 'GET',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
'Token' : TOKEN,
},
})
.then((response) => response.text())
.then((responseText) => {
resolve(responseText);
})
.catch((error) => {
reject(error);
});
});
};
私はフェッチ要求をし、私のhome.js画面に送信書きます。
var Home = React.createClass({
.
.
.
changeComponent: function(component) {
Method.getFriendFeed(this.state.tokenSupreme)
.then((res) => this.setState({
friendFeed: JSON.parse(res).friendPosts,
loaded: true,
}))
.catch((error) => console.log(error))
.done();
this.setState({
componentSelected: component,
})
},
特定のフィードビューを押すと、そのビューのコンテンツが具体的に読み込まれることを意味します。
コンポーネントをレンダリングし、情報フィードのと、それがロードされるかどうか小道具を渡す:
renderComponent: function(component) {
if(component === 'One') {
return <ComponentOne
friendData={
this.state.friendFeed
}
loaded={
this.state.loaded
} />
} else if(component === 'Two') {
return <ComponentTwo />
}
},
しかし今、本当の問題が来ます。 React Nativeによって提供されたドキュメントに従います。私はconsole.log
を持っていて、 "loading"を生成したrowDataを返し、それから、私が望むjson形式のデータを返します。つまり、これは最初にchangeComponent
の機能を正常に通過して「読み込み」になり、間違ってがtrue
になることを意味しますが、この実行中に特定のキーや名前を呼び出すことはできません私のjsonは、それがただ「種類の」ロードされているという事実のために、定義されていないだけ戻って、すぐにアプリケーションを赤くスクリーンします。
var ComponentOne = React.createClass({
getInitialState: function() {
var ds = new ListView.DataSource({rowHasChanged: (r1, r2) => r1 !== r2});
return {
dataSource: ds.cloneWithRows(this.props.friendData),
};
},
render: function() {
if (!this.props.loaded) {
return this.renderLoadingView();
} else {
return (
<ListView
dataSource={this.state.dataSource}
renderRow={this.renderRow}
style={styles.card} />
)
}
},
renderLoadingView: function() {
return (
<View style={{alignItems: 'center', alignSelf: 'center', marginTop: 150, justifyContent: 'center'}} >
<Text style={{fontSize: 30, fontWeight: '400', color: '#666'}}> Loading ... </Text>
</View>
);
},
renderRow: function(rowData) {
return (
<CardView
name={rowData.name}
fullname='xyz'
distance='xyz'
title={rowData.title}
message={rowData.description}
score='xyz'
stars='xyz'
starcolors={colors.General.black}
vouched="Example" />
)
}
})
なぜこれが私に与えられますか?フェッチャーをcomponentDidMountに入れてレンダリングすると、無限に発生します。
この実行/ライフサイクルの問題をより効率的に制御するにはどうすればよいですか?私はまた、if (this.props.friendFeed.friend.name !== undefined)
と言っているIF文にしようとしましたが、信じているかどうかは分かりませんが、this.props.friendFeed.friend.name is undefined
という赤い画面が返ってきます。キー内のオブジェクトを呼び出す際に問題になることはありますか?それが定義される前に、定義されていないものを呼び出すことができますか?