A
A
Alex Krynytskyi2021-01-24 19:13:41
React
Alex Krynytskyi, 2021-01-24 19:13:41

How to properly display data in React using map()?

Please tell me how to do it right or not?

let data = [
  {
    box: [
      {box: <div className="box"></div>, status: 'show'},
      {box: <div className="box"></div>, status: 'show'},
      {box: <div className="box"></div>, status: 'show'}
    ]
  },
  {
    box: [
      {box: <div className="box"></div>, status: 'show'},
      {box: <div className="box"></div>, status: 'show'},
      {box: <div className="box"></div>, status: 'show'}
    ]
  }
]



const App = (props) => {
  return (
    <div className="grid">
      {data.map((item)=>{
        return (
          <div className='card'>
            {item.box.map((items2)=>{
              {items2.box}
            })}
          </div>
        )
      })}
    </div>
  );
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim Bredikhin, 2021-01-24
@MaximBredikhin

Like this

const App = () => {
  const [items] = React.useState([
    { boxId: 0, id: 0, status: 'show' }, 
    { boxId: 0, id: 1, status: 'show' }, 
    { boxId: 1, id: 2, status: 'hue-moe' }
  ]);
  const [boxes] = React.useState([
    { id: 0 },
    { id: 1 }
  ]);

  return (
    <div className="grid">
      {boxes.map(({ id }) => 
        <ul className="box" key={id}>
          {items.filter(({ boxId }) => boxId === id)
            .map(({ id, status }) => <li key={id}>{status} {id}</li>)}
          </ul>)}
    </div>
    );
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question