I have this empty input here and I want to replace the icon and the text there with the image preview when an image is inserted in it. The problem. I want to remove the "Drop app logo……." when the image preview is there. It does’nt look neat the way it is right now
const images = files.map((file) => (
<img
key={file.name}
src={file.preview}
alt="image"
style={{ width: "50%", height: "50%" }}
/>
));
<Flex width={{ sm: "340px", md: "450px" }}>
{images && (<><div>{images}</div></>)}
{images !== null && (<><Box>
<AiOutlineCloudUpload size={30} />
</Box>
<Box>
<Text fontSize="sm"> Drop app logo here or{" "}
<Button variant="link" color="#002c8a">browse
</Button>
</Text>
</Box>
</>
)}
</Flex>
2
Answers
Your boolean logic is incorrect;
images
is an array, and in JavaScript, arrays (even empty arrays) are never falsy. So theimages && (<><div>{images}</div></>)
section will always render, becauseimages
is always truthy, but theimages !== null && (<><Box>...
block will always render too, becauseimages
is never null.Try something like this instead, to render images if images exist and to render the upload prompt if no images exist. (I’m also removing a spurious React fragment (
<>
).)If you want to remove the text when image is loaded
maybe you can add conditional inside
<Text></Text>
tagit will append empty string when image !== null