Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

214
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda