1

I have a list of names that I need to return with commas in between, so normally I'd just use .join, but one item in the list will need to have the icon of a star next to it based on if a condition is met, which doesn't work in the join. Is there something similar to a join that I could use that doesn't turn everything into a string so that it displays the HTML element instead of returning 'name, [Object, object]'? I tried using dangerouslySetInnerHTML, but it continues to just say name, [Object, object] instead of name, <Icon name='star' />name2 with an actual star displaying, not the text for the component.

1
  • You could wrap each name in a <span> which should allow you to place an <Icon /> as a sibling to the string. Then just render the array of spans like normal. Commented May 31, 2017 at 23:47

1 Answer 1

1

So if Icon is another React element, you should wrap each name is a <span> element so that as you loop through the array, you can add the Icon as a sibling to the correct string content.

It could look like this:

names.map((name, i) => {
   return <span key={i}>
           { 
            name === focusName ? // However you determine where the star goes
            <Icon name="star" /> :
            null
           }
           { // Add a comma to name if not last item
             name + (i < names.length - 1 ? ',' : '')
           } 
          <span>
});
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.