I am trying to render an image being called within an asynchronous function, but the value is not assigned based on API data response.
Code:
import React from 'react';
import { TouchableOpacity } from 'react-native';
import Icon from 'react-native-vector-icons/MaterialIcons';
import Icon2 from 'react-native-vector-icons/MaterialCommunityIcons';
import axios from 'axios';
import { Container, Left, Avatar, Info, Name, Time } from './styles';
export default function Appointments({ data }) {
asd = async () => {
const user = data.item.userLogin.toLowerCase();
const response = await axios.get(
`https://api4.successfactors.com/odata/v2/Photo?$filter=tolower(userId) eq '${user}'`,
{
auth: {
username: 'Apiuser',
password: 'Apipass',
},
}
);
const encodedData = response.data.d.results[0].photo.toString();
console.log(encodedData);
return encodedData;
};
return (
<Container>
<Left>
<Avatar
source={{
uri: `data:image/png;base64,${this.asd()}`,
}}
/>
{console.log(this.asd())}
<Info>
<Name>
{data.item.userOnDesk} {'\n'}({data.item.userLogin})
</Name>
<Time>
<Icon2 name="office-building" size={20} color="#000" />
Prédio: {data.item.Predio}
</Time>
<Time>
<Icon2 name="chair-school" size={20} color="#000" />
Mesa: {data.item.Desk}
</Time>
<Time>{data.item.Andar} ºAndar</Time>
</Info>
</Left>
<TouchableOpacity onPress={() => {}}>
<Icon name="person" size={20} color="#005BAF" />
</TouchableOpacity>
</Container>
);
}
How do I load the variable before executing a return? I need my method of rendering the image to be asynchronous, if I put the "Appointments" function asynchronous, it doesn't work.