Instead of using a static array below, I want to use a json file, so I can update the data myself. The cards array is used for the DeckSwiper component (see: https://docs.nativebase.io/Components.html#deckswiper-def-headref )
const cards = [
{
text: 'text1',
name: '1',
image: {
uri: 'http://www.xxx.xx/2.jpg'
}
},
{
text: 'text2',
name: '2',
image: {
uri: 'http://www.xxx.xx/2.jpg'
}
},];
The json I want to fetch looks like this.
{
"text1": "text1",
"name1": "1",
"image1": {
"uri1": "http://www.xxx.xx/1.jpg"
},
"text2": "text2",
"name2": "2",
"image2": {
"uri2": "http://www.xxx.xx/2.jpg"
}
}
So I want to use 'fetch' to fetch the json file. First by initiating the array in the constructor, like this.
constructor() {
super();
this.state = {
active: 'true',
response: [{text: '', name: '', image: {uri: ''}}],
}
}
Then I use 'fetch' in componentDidMount() like this.
componentDidMount() {
fetch("http://www.xxx.xx/test.json", { method: 'get' })
.then((response) => response.json())
.then((responseJson) => {
this.setState({ response: responseJson });
});
}
How can I convert the json to an array? Because in the code below the dataSource needs an array instead of an object.
render() {
return(
...
<DeckSwiper dataSource={this.state.response}
...
/>
);
}