0

I am new to React and JS.

I have this piece of code but 'rows' is undefined in console.log output. I am guessing my map function is incorrect but I can't figure out why.

const stubData = [
  { id: "435879430589041", customer: "jdus", status: "OK" },
  { id: "435879430589042", customer: "jdfr", status: "OK" },
  { id: "435879430589043", customer: "jdnl", status: "pending" },
  { id: "435879430589044", customer: "wsi", status: "config" },
  { id: "435879430589045", customer: "tkmaxx", status: "pending" },
];

const Rows = () => {
  const rows = stubData.map((c,i) => {
    (<tr key={i} value={c}>
      <td>{c.id}</td>
      <td>{c.customer}</td>
      <td>{c.status}</td>
    </tr>)
  }); 
  console.log(rows)
  return <tbody>{rows}</tbody>;
};

const ListIntegrations = props => {
  return (
    <table className="table table-hover">
      <Headers />
      <Rows />
    </table>
  );

Headers returns headers perfect.

3 Answers 3

3

You're not returning anything in the map. Either add a return statement:

  const rows = stubData.map((c,i) => {
    return (<tr key={i} value={c}>
      <td>{c.id}</td>
      <td>{c.customer}</td>
      <td>{c.status}</td>
    </tr>)
  }); 

...or delete the curly brackets, thus making the return implicit:

  const rows = stubData.map((c,i) => 
    (<tr key={i} value={c}>
      <td>{c.id}</td>
      <td>{c.customer}</td>
      <td>{c.status}</td>
    </tr>)
  ); 
Sign up to request clarification or add additional context in comments.

Comments

1

You are not returning from map

stubData.map((c,i) => {
    (<tr key={i} value={c}>
      <td>{c.id}</td>
      <td>{c.customer}</td>
      <td>{c.status}</td>
    </tr>)
  }); 

change it as

stubData.map((c,i) => {
    return (<tr key={i} value={c}>
      <td>{c.id}</td>
      <td>{c.customer}</td>
      <td>{c.status}</td>
    </tr>)
  }); 

Comments

0

As you are using arrow function as a callback to map. If you don't have curly braces {} arrow function implicitly returns

const rows = stubData.map((c,i) => 
(<tr key={i} value={c}>
  <td>{c.id}</td>
  <td>{c.customer}</td>
  <td>{c.status}</td>
</tr>)
);

Or if you use curly braces you need to return explicitly

const rows = stubData.map((c,i) => {
return
(<tr key={i} value={c}>
  <td>{c.id}</td>
  <td>{c.customer}</td>
  <td>{c.status}</td>
</tr>)
}); 

Comments

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.