I am using django as the backend, and react-native for the app. When the app is opened inside the react-native app, on componentDidMount()
, the method will will request through the url to the django server:
export default class App extends React.Component {
constructor(props) {
super(props)
}
componentDidMount() {
this.fromServer()
}
fromServer() {
var headers = new Headers();
headers.append('Accept', 'application/json');
let a = fetch('http://xxx.xxx.xx.xx:8080/posts/', headers)
.then(function(response) {
console.log('fetched...')
if (response.status !== 200) {
console.log('There was a problem. Status Code: ' + response.status);
return;
}
response.json().then(function(data) {
console.log(data);
});
}
)
.catch(function(err) {
console.log('Fetch Error :-S', err);
});
}
render() {
return (
<View>
<ListView dataSource=?????></ListView>
</View>
);
}
}
And the server will respond with an array of json objects. Like so:
[
{
"id": 7,
"target": {
"body": "This is the body",
"title": "Airbnb raising a reported $850M at a $30B valuation"
}
},
{
"id": 11,
"target": {
"body": "This is the body",
"title": "Browsing the APISSS"
}
}
]
Since I have enabled remote debugging, I can see the json objects in the console.
I know the basic of creating a ListView
. My problem is, when the array of objects are fetched, how can I use that array of objects to render it with the ListView
, so that I can display the title and body for each list item. Do I create a separate state in the constructor and add it to the dataSource? How do you integrate react-native app with the backend server?