Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

213
Views
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 answers
Answer question

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 Report

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!