0
votes

I am trying to make the multiple dynamic buttons selected from the string but it is not working with the comma-separated string.

My code

{thisLabels && thisLabels.map((names, index) => (
    <button type="button" className={`btn btn-outline-primary ${urltype === names ? "active" : ""}`} data-bs-toggle="button" autoComplete="off" name={names}>
        {names}
    </button>
))}

Works

It works for the single button if urlType is a single string (non comma-separated) for example "apple"

Doesn't work

It doesn't work if urlType has a comma-separated string for example "apple,banana,orange,etc"

Expected Result

If the urlType has the following values "apple, banana, orange" then

button-apple (active)

button-grapes (no active)

button-banana (active)

button-orange (active)

Hot tip: make thisLabels always an array (never null or undefined) and remove thisLabels &&. - Codebling
could you show what thisLabels actually looks like so I can test it on my own? - PRSHL
You should tokenize urlType. "Tokenize" is a fancy programming word which basically means split it up into an array. const types = urlType.split(/, */); then ${ types.includes('apple') ? 'active' : ''} - Codebling