I am writing a react native app. I am able to understand how to retrieve data from api data by using axios or fetch. But I am having trouble. I am trying to use my token I passed from the previous class and use that to fetch a new api and data. I am able to connect to the server and I connected fine in POSTMAN. But when I look in my log from terminal in Visual Studio. I get am empty array. I get array = []. But I know that is is supposed to show data.
async componentDidMount(){
try {
const savedProfile = await AsyncStorage.getItem('userData');
const profile = JSON.parse(savedProfile);
const token = profile.token;
console.log(token);
const email = profile.user_email;
const name = profile.user_nicename;
fetch(url, {
method: 'GET',
headers:{
Accept: 'application/json',
'Content-Type': 'application/json',
},
Authorization: "Bearer " + token,
})
.then(response => response.json())
.then(response => console.log(response));
} catch (err) {
console.warn(err);
}
};
I believe the issue might be in render as with the other posts on this site that I read. I looked at the documentation on the react website too but I may need a little more help. What I was trying to do first was just to print the data in the console. I filled my page with dummy data. But if possible I was hoping to fill my table with a specific part of the json data. Basically fetching address and this is an example.
"address": "4060 18th Ave NE, Naples, Florida, USA",
The rest of my render looks like this. But I am hoping to at least show the data in the console so I can go from there.
render() {
return (
<View>
<Header
centerComponent={{ text: 'Schedule', style: { color: '#fff' } }}
containerStyle={{
backgroundColor: '#000000',
justifyContent: 'space-around',
}}
/>
<FlatList
data={[
{key: 'Devin'},
{key: 'Dan'},
{key: 'Dominic'},
{key: 'Jackson'},
{key: 'James'},
{key: 'Joel'},
{key: 'John'},
{key: 'Jillian'},
{key: 'Jimmy'},
{key: 'Julie'},
]}
renderItem={({item}) => <Text style={styles.item}>{item.key}</Text>}
/>
</View>
);
}