Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

179
Visualizações
Unable to populate a list in reactjs from an array

I have a list app. I want its item to populate in MenuItem. I tried the following:-

{app.map((item, index)=>{
       <MenuItem value={item}>{item}</MenuItem>
})}

but I'm getting this error:-

Expected an assignment or function call and instead saw an expression

console.log(item) shows [ "NE", "NO" ]
console.log(app) shows [ [ "" , "", "" ], [ "NE", "N)" ] ]

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

You need to return something from map, In your case you have to return MenuItem as

Live Demo

Codesandbox Demo

Below will implicitely return the MenuItem

{
  app.map((item, index) => <MenuItem value={item}>{item}</MenuItem>);
  // without { and }
}

or You can explicitly return the MenuItem

{
  app.map((item, index) => {
    console.log(item);
    return <MenuItem value={item}>{item}</MenuItem>;
  });
}

EXPLANATION

Since, you are using map here which should be used if we want to map over the array and return something from it and map will return an array of elements and what you want is to map over the app array and return MenuItem elements because whatever you will return from map will eventually become React children.

return (
        <div>
            {app.map((item, index) => {
                (
                    <MenuItem value={item} key={item}>
                        {item}
                    </MenuItem>
                );
            })}
        </div>
    );

Above code snippet gets converted to

return /*#__PURE__*/React.createElement("div", null, app.map((item, index) => {
    /*#__PURE__*/
    React.createElement(MenuItem, {     // NOTICE, you are not
      value: item,                      // returning any React
      key: item                         // Elements from here
    }, item);
  }));

So, you should return from map

about 4 years ago · Juan Pablo Isaza Relatório

0

2 Issue

  1. console.log(item), to console.log(item); // comma to semicolon
  2. <MenuItem value={item}>{item}</MenuItem > to return <MenuItem value={item}>{item}</MenuItem> // need to return
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda