![](/img/trans.png)
[英]I get a JSON with fetch in react-native how can i send the results to other components
[英]How Can I get Nested JSON to my DataSource … React Native
幫我。 我如何才能將嵌套JSON傳遞到我的數據源... React Native為什么這么難或者我是新手,我無法做到這一點。
{
success: true,
result:
{
types: [],
information: {
Campaigns: {
isEntity: true,
label: "Campaigns",
singular: "Campaign"
},
Vendors: {
isEntity: true,
label: "Vendors",
singular: "Vendor"
},
ProductTaxes: {
isEntity: false,
label: "ProductTaxes",
singular: "ProductTaxes"
}
}
}
}
我可以在hasNext下使用Iterator在Java(Native Android)中完美地做到這一點
但是在React Native中我不能這樣做:(
我需要獲取標簽以在Listview中顯示,條件是isEntity =“ true” is isEntity =“ false”不要在Listview中顯示
import React, { Component } from 'react';
import {
Text,
View,
ScrollView,
ActivityIndicator,
ListView
} from 'react-native';
import { List, ListItem } from 'react-native-elements';
import { users } from '../config/data';
class Feed extends Component {
constructor(props){
super(props)
this.state = {
mSessionName:null,
isLoading: true,
};
}
onLearnMore = (user) => {
this.props.navigation.navigate('Details', { ...user });
};
componentWillMount() {
const {state} = this.props.navigation;
console.log(state.params.sessionName);
console.log(state.params.u_url);
console.log(state.params.u_url+"/webservice.php?operation=listtypes&sessionName="+state.params.sessionName);
return fetch(state.params.u_url+"/webservice.php?operation=listtypes&sessionName="+state.params.sessionName)
.then((response) => response.json())
.then((responseJson) => {
let ds = new ListView.DataSource({rowHasChanged: (r1, r2) => r1 !== r2});
this.setState({
isLoading: false,
dataSource: ds.cloneWithRows(responseJson.result),
}, function() {
// do something with new state
});
})
.catch((error) => {
console.error(error);
});
}
render() {
if (this.state.isLoading) {
return (
<View style={{flex: 1, paddingTop: 20}}>
<ActivityIndicator />
</View>
);
}
return (
<View style={{flex: 1, paddingTop: 20}}>
<ListView
dataSource={this.state.dataSource}
renderRow={(rowData) => <Text>{rowData.information.Campaigns}, {rowData.information.Campaigns}</Text>}
/>
</View>
);
}
}
export default Feed;
輸出:>> 我的輸出img
您可以將條件添加到renderRow:
renderRow={(rowData) => <Text>{rowData.information.Campaigns}, {rowData.information.Campaigns}</Text>} // not sure why repeating
但根據您的評論,它應該是:
renderRow={(rowData) => <Text>{rowData.information.Campaigns.isEntity ? rowData.information.Campaigns.label : </view> }</Text>}
如果需要顯示更多數據,則建議您制作函數以渲染行並添加需要顯示數據的視圖結構。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.