Say I have a JS
file that contains a dictionary (an Object full of key: value
pairs) and is exported to a JSX
file.
How can I create a function that gets a string and returns a span with every word that exists both in the dictionary's keys
and the string converted into a tooltip with the matching value
as its content embedded with the rest of the non-tooltipped text (like Wikipedia, for reference)?
I've found this tooltip library from react-mui
, so creating the tooltip itself is not the problem, but only creating the function to convert all the occurrences automatically.
Below is the function that gets the key
(name) and value
(explanation) and converts them to a Tooltip:
function defToToolTip(name, explanation) {
return <DictionaryTooltip title={explanation}>{name}</DictionaryTooltip>;
}
I've tried to create a function that gets a string and embeds tooltips wherever a word from the dictionary appears but got stuck very early.
I need help thinking about how to make sure all the words in the string that are also in the dictionary are converted using the function above.