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

159
Vistas
set childNodes dynamicly styles with array of colors in reactjs

I want to give each div or childNodes a different color using an array of colors But only the last color of the array is given to all the divs or childNodes.

Can you tell me how we can dynamically style them by mapping or forEach nodes,childNodes,dives and colors in reactjs?

you can see demo: codesandbox link

const containerRef = useRef()

    useEffect(()=>{
        containerRef.current.childNodes.forEach(item=>{
          ['green','blue','green','yellow','orange'].map(colors => {
            item.style.background = colors 
         })
        })
      })


    <div 
          ref={containerRef }
          className="h-96 w-full flex items-center gap-x-6 border border-black">
            <div className='w-20 h-20 border border-black'>one</div>
            <div className='w-20 h-20 border border-black'>two</div>
            <div className='w-20 h-20 border border-black'>three</div>
            <div className='w-20 h-20 border border-black'>four</div>
            <div className='w-20 h-20 border border-black'>five</div>
          </div>
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

You don't need map the colors, you just need to assign each element with a corresponding index of color.

import "./styles.css";
import React, { useEffect, useRef } from "react";

export default function App() {
  const containerRef = useRef();
  const colors = ["green", "blue", "green", "yellow", "orange"];

  useEffect(() => {
    containerRef.current.childNodes.forEach((item, i) => {
      item.style.background = colors[i];
    });
  });
  return (
    <div ref={containerRef} className="container">
      <div>one</div>
      <div>two</div>
      <div>three</div>
      <div>four</div>
      <div>five</div>
    </div>
  );
}

live demo

about 4 years ago · Juan Pablo Isaza Denunciar

0

Here you're mapping again on colors, for each item in child nodes which results in assigning the last color to each div.

You will have to use the index of the item in the childNodes list, and access the color of that index from the colors array.

CodeSanbox Link - https://codesandbox.io/s/fragrant-frost-3v0v76

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