Как выделить слово при наведении курсора мыши в тексте в приложении React
Код: Выделить всё
const text = data[data.length - 1] !== undefined ? data[data.length - 1].taskText : null;
const [hilightWord, setHilightWord] = useState(false);
const hightlight = (text, hilightWord) => {
if (!hilightWord) return text;
const regex = new RegExp(`(${hilightWord})`, "gi");
return text.split(regex).map((substring, i) => {
return (
{substring}
);
});
};
setHilightWord(true)} onMouseLeave={() => setHilightWord(false)}>{hightlight(text, hilightWord)}
Источник: https://stackoverflow.com/questions/781 ... -react-app