Skip to content

Commit

Permalink
fix: handle an error when highlighting an unmount component, occurs w…
Browse files Browse the repository at this point in the history
…hen navigating between tabs
  • Loading branch information
felixmosh committed Oct 31, 2024
1 parent 7cad06e commit 9add4ed
Showing 1 changed file with 11 additions and 13 deletions.
24 changes: 11 additions & 13 deletions packages/ui/src/components/Highlight/Highlight.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,17 @@ interface HighlightProps {
export const Highlight: React.FC<HighlightProps> = ({ language, text }) => {
const [code, setCode] = useState<string>('');

const textToCode = async () => {
setCode(await asyncHighlight(text as string, language));
};

useEffect(() => {
textToCode();
}, []);

useEffect(() => {
textToCode();
let unmount = false;
asyncHighlight(text as string, language).then((newCode) => {
if (!unmount) {
setCode(newCode);
}
});

return () => {
unmount = true;
};
}, [language, text]);

const handleCopyClick = () => {
Expand All @@ -35,10 +36,7 @@ export const Highlight: React.FC<HighlightProps> = ({ language, text }) => {
<code className={cn('hljs', language)} dangerouslySetInnerHTML={{ __html: code }} />
</pre>

<Button
onClick={handleCopyClick}
className={s.copyBtn}
>
<Button onClick={handleCopyClick} className={s.copyBtn}>
<CopyIcon />
</Button>
</div>
Expand Down

0 comments on commit 9add4ed

Please sign in to comment.