I would like to give a default value to my component Avatar from React Native Elements because I have not prepared all the icons files required. However, when I use the OR logic, it fails to work. What is the proper way to do that?
{sites.map((key) => {
return (
<Avatar
size={32}
containerStyle={{ margin: 0, padding: "0.1em" }}
rounded
source={
require("../../assets/" + utils.getPathName(key.properties.nameEN, "icon.png")) ||
require("../../assets/default/icon.png")
}
onPress={() => {
displayModal(key);
}}
/>
);
})}
2
Answers
This is what I would do:
If your icons will change through time, you’ll have to use state.
Also, you may want to use
Image
instead ofAvatar
to better handleonError
events.