I have a list of colors and I'm trying to get the current color onClick from the list but instead I get the entire list of colors. I have a child component where I'm mapping the array:
function Child({colorList, setBackgroundColor}){
return(
<Box>
{colorList.map((i,key)=>
<List key={key}>
<ListItem >
<IconButton icon={<FiCircle fill={i}/>} onClick={setBackgroundColor} color={i}/>
</ListItem>
</List>
</Box>
)
}
and I'm passing the values in the parent component:
function Parent(){
const [bgColor,setBgColor] = useState('')
const [colorList, setColorList] = useState(['blue','red','green'])
function setBackgroundColor(){
const selectedValue = colorList.filter((color,key)=>color[key])
setBgColor(selectedValue)
console.log(bgColor)
}
return(
<Child colorList={colorList} setBackgroundColor={setBackgroundColor}/>
)
}
When I'm explicitly passing the index setBgColor(selectedValue[0]) it works. Can someone please help me understand what I'm doing wrong? Any help would be appreciated!