From 3fd5fd85682db27d7b5f49e33b2f50df9fb5873f Mon Sep 17 00:00:00 2001 From: Kieran Date: Sat, 14 Jan 2023 18:50:24 +0000 Subject: [PATCH] Cache transformed body --- src/element/Text.js | 37 ++++++++++++++++++++----------------- 1 file changed, 20 insertions(+), 17 deletions(-) diff --git a/src/element/Text.js b/src/element/Text.js index 3d83efd8..123625b4 100644 --- a/src/element/Text.js +++ b/src/element/Text.js @@ -9,6 +9,7 @@ import LazyImage from "./LazyImage"; import Hashtag from "./Hashtag"; import './Text.css' +import { useMemo } from "react"; function transformHttpLink(a) { try { @@ -37,11 +38,11 @@ function transformHttpLink(a) { return e.stopPropagation()}>{url.toString()} } } else if (tweetId) { - return ( -
- -
- ) + return ( +
+ +
+ ) } else if (youtubeId) { return ( <> @@ -141,16 +142,16 @@ function extractHashtags(fragments) { } function transformLi({ body, tags, users }) { - let fragments = transformText({ body, tags, users }) - return
  • {fragments}
  • + let fragments = transformText({ body, tags, users }) + return
  • {fragments}
  • } function transformParagraph({ body, tags, users }) { - const fragments = transformText({ body, tags, users }) - if (fragments.every(f => typeof f === 'string')) { - return

    {fragments}

    - } - return <>{fragments} + const fragments = transformText({ body, tags, users }) + if (fragments.every(f => typeof f === 'string')) { + return

    {fragments}

    + } + return <>{fragments} } function transformText({ body, tags, users }) { @@ -162,11 +163,13 @@ function transformText({ body, tags, users }) { } export default function Text({ content, tags, users }) { - const components = { - p: (props) => transformParagraph({ body: props.children, tags, users }), - a: (props) => transformHttpLink(props.href), - li: (props) => transformLi({ body: props.children, tags, users }), - } + const components = useMemo(() => { + return { + p: (props) => transformParagraph({ body: props.children, tags, users }), + a: (props) => transformHttpLink(props.href), + li: (props) => transformLi({ body: props.children, tags, users }), + }; + }, [content]); return {content} }