0

I have a map that looks like this

data

I am using .map() to try to produce them as images like so:

{theLinks.map((item, indx) => (
  <img key={indx} src={item.src} alt={item.label} />
))}

Nothing is getting returned, if I mess with it I can get a single img to return with no valid source and the alt is "unknown".

1
  • Please edit your question to show where theLinks is defined and assigned data. Commented Jun 5, 2022 at 23:12

2 Answers 2

2

make sure to add your mapped array theLinks after or inside a return function,
for example, this will NOT work :

export default function App() {
  const theLinks = [
    { lable: "Daily Mix", src: "https://flif.info/example-images/fish.png" },
    { lable: "Legit", src: "https://flif.info/example-images/fish.png" },
    { lable: "SCL", src: "https://flif.info/example-images/fish.png" }
  ];

  {theLinks.map((item, indx) => (
        <img
          style={{ width: "50%", border: "1px solid #ccc" }}
          key={indx}
          src={item.src}
          alt={item.label}
        />
  ))}
}

SOLUTION 1
this will work (renders the mapped array and other elements too):

export default function App() {
  const theLinks = [
    { lable: "Daily Mix", src: "https://flif.info/example-images/fish.png" },
    { lable: "Legit", src: "https://flif.info/example-images/fish.png" },
    { lable: "SCL", src: "https://flif.info/example-images/fish.png" }
  ];

  return (
    <>
      <h1> thanks Phil for your suggestion! </h1>
      {theLinks.map((item, indx) => (
        <img
          style={{ width: "50%", border: "1px solid #ccc" }}
          key={indx}
          src={item.src}
          alt={item.label}
        />
      ))}
      ;
    </>
  );
}

SOLUTION 2
this will work (renders only the mapped array)

export default function App() {
  const theLinks = [
    { lable: "Daily Mix", src: "https://flif.info/example-images/fish.png" },
    { lable: "Legit", src: "https://flif.info/example-images/fish.png" },
    { lable: "SCL", src: "https://flif.info/example-images/fish.png" }
  ];

  return theLinks.map((item, indx) => (
    <img
      style={{ width: "50%", border: "1px solid #ccc" }}
      key={indx}
      src={item.src}
      alt={item.label}
    />
  ));
}
Sign up to request clarification or add additional context in comments.

3 Comments

codesandbox.io/s/thelinks-x6h5pk?file=/src/App.js check this out to understand what i mean it has to be inside a return function
The React fragment (<> ... </>) is redundant
@Phil thanks for your comment, i did some edits and explained why React fragment might be useful :)
-1

Try this

{theLinks.map((item, indx) => { return ( <img key={indx} src={item.src} alt={item.label} /> ); })}

2 Comments

It's an arrow function so it returns by default unless it encounters a {, and he used { to wrap so an explicit return is not needed.
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.