1

i am having an error with below code. it shows module build failed. I think the structure is missing some tag

<tbody>
          {
            ListItems.map((section) =>
              <tr key={section.id}>
                <td>{section.name}</td>
              </tr>
                  {
                section.items.map((item) =>
                
                  <tr key={item.id}> // the error is here
                    <td>{item.productName}</td>
                  </tr>
                )})}
        </tbody>

1 Answer 1

0

You need to use proper paranthesis and wrap everything inside fragment as follows.

<tbody>
  {
    ListItems.map((section) => (
      <Fragment key={section.id}>
        <tr key={section.id}>
          <td>{section.name}</td>
        </tr>
          {
             section.items.map((item) => (
               <tr key={item.id}>
                   <td>{item.productName}</td>
               </tr>
             )
           }
      </Fragment>
      )
    }
</tbody>
Sign up to request clarification or add additional context in comments.

5 Comments

I tried this. but i have key error
Try this and also post the exact error.
react-jsx-dev-runtime.development.js:87 Warning: Each child in a list should have a unique "key" prop. Check the render method of PriceList. See reactjs.org/link/warning-keys for more information. at PriceList at div at App
This is a warning, means that items do not have unique id.
i got a solution by adding <React.Fragment key={section.id}> instead of empty tag

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.