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

202
Views
Add a child component with N number .of times inside parent Component (based on a state value)

I want to add a child-component ColorBox in return of parent component ColorBoxContainer based on No of times there is a value in state noOfBoxes: 16. I am trying doing using for-loop but I guess the code is wrong. Can someone help me out, how to achieve this?

import React, { Component } from 'react';
import ColorBox from './ColorBox';

class ColorBoxContainer extends Component {

  constructor(props) {
    super(props);
    this.state = {
      noOfBoxes: 16
    }
  }

  render(){
    return (
      <div>
        {for(i=0;i<this.state.noOfBoxes;i++){
           <ColorBox />
        }}
      </div>
    )
  }  
}

export default ColorBoxContainer; 
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

Create an array with the given element length and map it to your element:

<div>
  {Array(this.state.noOfBoxes).fill().map((_, index) => (
    <ColorBox key={index} />
  ))}
</div>
about 4 years ago · Juan Pablo Isaza Report

0

import React, { Component } from 'react';
import ColorBox from './ColorBox';

class ColorBoxContainer extends Component {

  constructor(props) {
    super(props);
    this.state = {
      noOfBoxes: 16
    }
  }

  render(){
    return (
      <div>
      { 
        Array(this.state.noOfBoxes).fill().map((_,i) =>  <ColorBox key={i}/>)
      }
      </div>
    )
  }  
}

export default ColorBoxContainer; 

about 4 years ago · Juan Pablo Isaza Report

0

return Array.from({length: this.state.noOfBoxes}, (item, index) => 
  <ColorBox />
)
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!