
Is this code a correct way to import an image in React?
import avatar from "./assets/avatar.png" function UserImage() { return ( <div> <img src={avatar} alt = "User image" /> < /div> ) } export default UserImage;Yes
No
Is this code a correct way to import an image in React?
function UserImage() { const avatarImg = "https://picsum.photos/400/265"; return ( <div> <img src="avatar.png" alt="User image" /> </div> ) } export default UserImage;Yes
No
What's wrong with this code?
function ProfileImage() { const profileImg = "https://picsum.photos/400/265"; return <img src={profileImg} alt="Profile image" /> } export default ProfileImage;Nothing. This code is correct.
There should be parentheses after the return keyword and the img element should spread its attributes over multiple lines.
You must surround the img element with a root, wrapping div element.
About the author
A passionate full-stack developer from @ePlus.DEV
Comments
Join the discussionNo comments yet. Be the first to comment.
React Basics
In this module you will explore the basic structure and use of the React.js library. You will learn how to produce single page web applications using React components and to use JSX to style them.





