I want to render JSX like the following:
<p>
Some text with a <span> match </span> in it
</p>
My thought is to prepare data like this:
var text = ["Some text with a", <span>match</span>, "in it"]
return <p> {text} </p>
But this leads to problems with React keeping track of iterated children lacking keys.
Each child in an array or iterator should have a unique "key" prop.
Any idea how I could solve this problem?
Edit: to give some sense for how this is used, check out the following function stub:
/**
* prepares string with JSX span tags to assist with highlighting of search result matches
* @param {string} name string to find the match within
* @param {string} matchString the search query string that we are highlighting against
*
* @returns {Array} an array containing characters and JSX for syntax highlighting
*/
export const matchHighlighting = (name, matchString) => {...}
I know the search query and the response text and want to display to the user highlights -- I essentially attempted to write a function that would interpolate the contents of a string with span tags where necessary, leaving me with an array similar to the example that I posted at the top.
This react issues thread kind of highlights the problem; sounds like it's a tough one that hasn't really found a good solution?
spanto it, right?