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

159
Visualizações
React function return only one <option>

I want to create multiple options with the items in the "championList" but my function only returns one option with "Aatrox" and ignore the others, what should I do?


class ChampionSelect extends React.Component {
    constructor(props) {
        super(props);

        this.state = {
            champion: require('../loldata/champion.json'),
            championList: ['Aatrox', 'Ahri', 'Akali', 'Akshan', 'Alistar', 'Amumu', 'Anivia', 'Annie', 'Aphelios', 'Ashe', 'Aurelion', 'Azir', 'Bard', 'Blitzcrank' ]
        };

        this.funcOption = this.funcOption.bind(this);
    }

    funcOption() {
        for (let i = 0; i < this.state.championList.length; i++) {
            return <option value={this.state.championList[i]}>{this.state.championList[i]}</option>
        }
    }
    
    render() {
        return (
            <div> teste:
            <select>
                {this.funcOption()}
                <option>teste</option>
            </select>
            </div>
        );
    }
}

export default ChampionSelect;```
about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

You can do

this.state.championList.map((item) => (
   <option value={item} key={item}>{item}</option>
))

Directly into the JSX.

The main purpose of foreach it's to iterate, not creating something.

That's why you should use a map in your case.

When you use the "return" you go out from your method, so, that's why it wrote only 1 time.

about 4 years ago · Juan Pablo Isaza Relatório

0

you want something like this instead of funcOption in your select do this:

{
    this.state.championList.map(champ => (
        <option value={champ}>{champ}</option>
    ));
}

the reason your funcOption is returning only one option is because you returned from the function in your first iteration of the for loop. If you saved the options to a list then returned that at the end of funcOption it would also work!

about 4 years ago · Juan Pablo Isaza Relatório

0

Try to use map() instead of for loop.

{
  this.state.championList.map(champion => (
    <option value={champion}> 
  {champion}</option>
))
}
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