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

217
Visualizações
React: Map over array and dynamically render a component in a specific div

Probably I'm missing something really simple, but:

I have an array:

const [weight, setWeight] = useState([]);

And I want to map over it to dynamically render a component:

const renderWeight = () => {
    weight.map((e) => {
      <Weight />;
    });
  };

I want this to happen when I click on a submit button, and I want the components to render in a specific div. I can't find a way to do this.

Thank you!

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

0

I thing you need to return the mapped array

const renderWeight = () => {
   return weight.map((e) => {
      <Weight />;
    });
  };


note: Make sure to assign a key to each component rendered

about 4 years ago · Juan Pablo Isaza Relatório

0

You can use a show variable as toggler to show/hide the weight component like this

import React, { useState } from "react";

const App = () => {
  const [weight, setWeight] = useState([1, 2, 3]);
  const [show, setShow] = useState(false);

  return (
    <div className="container">
      <div className="weight">{show && weight.map((e) => <Weight />)}</div>
      <button onClick={() => setShow(!show)}>Click to show/hide</button>
    </div>
  );
};

export default App;

about 4 years ago · Juan Pablo Isaza Relatório

0

You have to return it.

const [show ,setShow] = useState(false)

const renderWeight = () => {
   return weight.map((e) => {
       return <Weight />;
    });
};

  return (
  <>
  <button onClick={() => setShow(!show)}>Click to show/hide</button>
   {
    show?
       renderWeight():''
   }
  </>

);

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