1

How can I display my product.colors ? I need to parse them first that's why I'm trying with forEach somehow:

Here is what I tryed:

    const ParsedColors = props => {
            return(
            props.product.color.forEach(col => {
                const parsed = JSON.parse(col)
                return(
                    <button name="color" value={parsed.value} style={{backgroundColor: `${parsed.value}`}} onClick={() => colorPicker([props.product._id, parsed.value])}/>
                )
            })
            )
        }
...

<div>
    <ParsedColors product={product}/>
<div/>

I know that forEach isn't returning an array but I can't find a way to do it in map. As I said, I need to parse each color

This returns an err:

Error: ParsedColors(...): Nothing was returned from render. This usually means a return statement is missing. Or, to render nothing, return null.

1
  • forEach doesn't return anything. You need to use map if you want to assign the iteration change to a variable Commented May 16, 2021 at 17:03

1 Answer 1

1

forEach returns nothing so you cannot assign a variable to it the way you are doing it. Use map instead

const ParsedColors = props => {
            return(
            props.product.color.map(col => {
                const parsed = JSON.parse(col)
                return(
                    <button name="color" value={parsed.value} style={{backgroundColor: `${parsed.value}`}} onClick={() => colorPicker([props.product._id, parsed.value])}/>
                )
            })
            )
        }
...
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.