0

I want an app.js that imports a component, that generates a row in a table for each element in an array.

import myRows from './myRows';
const myApp = ({ values}) => {
return (
   
    <div>
  <div>        
        {values.map((entry, index) => {
            return <myRows entry={entry} key={index}/>
        })}
    </div>
    </div>

)

} ...

const myRows = ({ entry}) => {
    return (

      **ROW ENTRY like row name = entry.name**


    )
}

...

Maybe I should create in the my App.js a table and create in the map function a row for each entry in values? Idk how to do that. I want a simple table where each row is created with each entry in values My idea is not working and mybe it is stupid and there are many better ways..

import myRows from './myRows';
const myApp = ({ values}) => {
return (
        <table>
                <thead>
                    <tr>
                        <th>1</th>
                        <th>2</th>
                        <th>3</th>
                        <th>4</th>
                        <th>5</th>
                        <th>6</th>
                        <th>7</th>
                    </tr>
                </thead>
                <tbody>
                     {values.map((entry, index) => {
            return <myRows entry={entry} key={index}/>
        })}
                </tbody>
            </table> 
       
   

)

} ...

 const myRows = ({ entry}) => {
    return (

                    <tr>
                        <th>entry.name</th>
                        <td>entry.secondName</td>
                        <td>C</td>
                        <td>D1</td>
                        <td>entry.value22</td>
                        <td>E3</td>
                        <td>E4/</td>
                    </tr>


    )
}

...

1
  • What is the problem with this code ? I can already see that there is an error in your myRows component, you have to use brackets to access variables in the HTML, like this: <th>{entry.name}</th> Commented Aug 24, 2022 at 13:20

1 Answer 1

1

Don't use index as key. Each entry should have a unique identifier; either an id or you can interpolate some values to create a unique key for each row (e.g key={${row.name}_${row.createdAt}}

const App = ({ array }) => {
  return <table>
    <tbody>
      { array.map(row => <Row data={row} key={row.id} />) }
    </tbody>
  </table>
}

const Row = ({ data }) => {
  return <tr>
    <td>{data.name}</td>
    <td>{data.created}</td>
    ...
  </tr>
}
Sign up to request clarification or add additional context in comments.

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.