JCrew0
JCrew0

Reputation: 129

Is there a way to combine string and span elements into one coherent element?

Currently I'm working in a version of React and have an array of strings and JSX.Elements. For example, the array would look like this: [<span class="name">JCrew0</span>, "edited this document"]. I'm then trying to display the elements of the array next to each other to make a sentence. However, because each element is on its own, the wrapping applies to each element separately, causing the spans and the strings to wrap by themselves. This then causes all sorts of weird formatting. Is there any way to combine these array elements into one JSX.Element while still applying the styles from class="name" onto the names (wrapping them in an outer div does not work)?

Upvotes: 0

Views: 127

Answers (1)

ColdDarkness
ColdDarkness

Reputation: 331

Perhaps you need something like this (if I assume correctly this small array is just array in array and it will stand this way):

arr.unshift(<p>);
arr.append(</p>);
const elementString = arr.join('')

Upvotes: 1

Related Questions