I have a FragmentedString class, which is a string with one or multiple distinct substrings.
class FragmentedString {
str: string;
fragments: Array<{
id: string;
start: number;
end: number;
}>
}
All the verification needed to ensure this fragmented string is valid has been done previously. Let's say we have the following FragmentedString, and every word "substring" is a substring.
This is a beautiful string with a few substrings, such as this substring and this other substring.
This is a beautiful string with a few [substring]s, such as this [substring] and this other [substring].
If I had to wrap every substring in an <a> tag, I would insert them and set dangerouslySetInnerHtml on a div. But what if I wanted to wrap these in React components? How can I do that? I thought of eval, which not only sounds like a bad idea, but I'm not sure if it supports either TypeScript or JSX. What's your take?
Edit
To avoid the XY problem: I would like the state of a parent component to be changed depending on the substring clicked. It would be something like that in React.
class Component {
handleEvent(e) {
// use e.target.name to extract the substring id, and set the parent state using a method passed as a prop
}
render() {
return (
<p>String <a name="substring_id" onClick={this.handleEvent.bind(this)}>substring</a></p>
)
}
}
Note that even though I use <a> here with its name property for storing the id, it may not be the right decision, I let you judge.
FragmentedStringwithout anydangerouslySetInnerHtml?