React img src attribute

Why do we have to wrap the image tag into some other tag inorder to read data that has been passed to it when exported as a prop .Eg
<p><img src={props.src} alt=""/></p>

You don’t have to wrap it.

function Image(props) {
  return <img src={props.src} alt={props.alternativeText}/>;
}

function App() {
  return (
    <>
      <p>This is an image</p>
      <Image src="https://placehold.co/200" alternativeText="placeholder image"/>
    </>
  );
}

But you should be returning a single root element (The Rules of JSX).


You are going to have to provide more context for what you are doing and what it is that doesn’t work for you.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.