0

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!

1 Answer 1

1

First - don't call a first param in map method as "i" if you do map for colors then it is 'color'

Second - You can write:

 <IconButton
  icon={<FiCircle fill={i} />}
  onClick={() => {
    setBackgroundColor(i);
  }}
  color={i}
/>

and you function setBackgroundColor will get your color when you click on that

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.