2

I had a static code that was working just fine, but when I tried to use a map() over a array to produce the same result dynamically, it doesn't work anymore.

I think that I made a syntax mistake when mapping my array. Can someone point out what is my mistake?

Here is the old static code that works fine:

const GridContent = (props)=>{
    return (
        <div className={classes.Container}>
           <ProgressBar percentage="100" styleName="ProgressBar1"></ProgressBar>
           <ProgressBar percentage="24" styleName="ProgressBar2"></ProgressBar>
           <ProgressBar percentage="48" styleName="ProgressBar3"></ProgressBar>
        </div>
    );
}

Here is what I tried to make it dynamic:

const fakeData3=[{percentageItem: '100'}, {percentageItem: '24'}, {percentageItem: '48'}, {percentageItem: '12'}, {percentageItem: '90'}, {percentageItem: '57'}, {percentageItem: '72'}, {percentageItem: '50'}, {percentageItem: '39'}]

const GridContent = (props)=>{
    return (
        <div className={classes.Container}>
            {fakeData3.map((item, index)=>{
                return <ProgressBar percentage={`"${item.percentageItem}"`} styleName={`"ProgressBar${index+1}"`}></ProgressBar>
            })}
        </div>
    );
}

I think there is a syntax mistake on the return of my last part of code ...

1
  • Why do you think there's a mistake? Is something not working in some way? Commented Jan 18, 2021 at 16:09

3 Answers 3

3

I think you have overcomplicated your quotes here -

<ProgressBar percentage={`"${item.percentageItem}"`} styleName={`"ProgressBar${index+1}"`}></ProgressBar>

try -

<ProgressBar percentage={item.percentageItem} styleName={`ProgressBar${index+1}`}></ProgressBar>
Sign up to request clarification or add additional context in comments.

2 Comments

It works perfectly !! Many thanks :) I thought I needed to display the " " to keep the same structure than when it was not dynamic. Why is it now useless ?
attributes/props in react can either be object based e.g myProp={myValue} or string based e.g. myProp="myValue", but since you need to use string template to inject some value using ${val} you have to use the object notation along with template literal string.
2

You're mixing quotations. The right code should be:

return <ProgressBar percentage={item.percentageItem} styleName={`ProgressBar${index+1}`} />
  1. percentage is already a string, so you can use it directly as {item.percentageItem}

  2. If you do `"something"`, styleName is actually becoming "\"something\"" (notice the escaped ") internally.

Comments

-1

You have to remove the redoundant double quotes:

{fakeData3.map((item, index) => (
    <ProgressBar percentage={item.percentageItem} styleName={`ProgressBar${index+1}`} />
))}

2 Comments

the OP has return which does the same job within {}
@andymccullough Oh yes you are right, that is not the problem here then. It is most likely due to the double quotes then

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.