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>