today I faced a problem with highlighting matching parenthesis in React. This is example text:
(my text (is here and) I want (to highlight (something here)))
And I want it to look like in code editor, I mean: image attached
I tried to use react-process-string:
processString([
{
regex: /\(|\)/g,
fn: (key, result) => this.colorBracket(key, result[0])
}
])(value);
colorBracket = (key, result) => {
const { usedColors } = this.state;
const bracketColors = ['red', 'green', 'yellow', 'blue', 'purple'];
let newColor = '';
if (result === '(') {
newColor =
usedColors.length
? bracketColors[usedColors.length]
: bracketColors[0];
if (!usedColors.includes(newColor)) {
this.setState({ usedColors: [...usedColors, newColor] });
}
} else {
newColor = usedColors.length
? usedColors[usedColors.length - 1]
: bracketColors[0];
if (usedColors.length) {
this.setState({ usedColors: usedColors.filter(e => e !== newColor) });
}
}
return <span style={{ color: newColor }}>{result}</span>;
};
but I faced problem with react maximum update depth.
Is it possible to do it more simple, without updating state and so on?