From 4f04184f97c553d2adf3b2a42d36b689c712d71f Mon Sep 17 00:00:00 2001 From: dan Date: Sun, 25 Sep 2022 18:55:39 +0100 Subject: [PATCH] [Beta] Optimize editor re-renders (#5113) --- .../components/MDX/Sandpack/CustomPreset.tsx | 72 ++++++++++++++----- 1 file changed, 55 insertions(+), 17 deletions(-) diff --git a/beta/src/components/MDX/Sandpack/CustomPreset.tsx b/beta/src/components/MDX/Sandpack/CustomPreset.tsx index 58822245..97951ccb 100644 --- a/beta/src/components/MDX/Sandpack/CustomPreset.tsx +++ b/beta/src/components/MDX/Sandpack/CustomPreset.tsx @@ -1,7 +1,7 @@ /* * Copyright (c) Facebook, Inc. and its affiliates. */ -import {useRef, useState} from 'react'; +import {memo, useRef, useState} from 'react'; import {flushSync} from 'react-dom'; import { useSandpack, @@ -18,7 +18,7 @@ import {Preview} from './Preview'; import {useSandpackLint} from './useSandpackLint'; -export function CustomPreset({ +export const CustomPreset = memo(function CustomPreset({ showDevTools, onDevToolsLoad, devToolsLoaded, @@ -30,19 +30,47 @@ export function CustomPreset({ providedFiles: Array; }) { const {lintErrors, lintExtensions} = useSandpackLint(); - const lineCountRef = useRef<{[key: string]: number}>({}); - const containerRef = useRef(null); const {sandpack} = useSandpack(); const {code} = useActiveCode(); - const [isExpanded, setIsExpanded] = useState(false); - const {activeFile} = sandpack; + const lineCountRef = useRef<{[key: string]: number}>({}); if (!lineCountRef.current[activeFile]) { lineCountRef.current[activeFile] = code.split('\n').length; } const lineCount = lineCountRef.current[activeFile]; - const isExpandable = lineCount > 16 || isExpanded; + const isExpandable = lineCount > 16; + return ( + + ); +}); +const SandboxShell = memo(function SandboxShell({ + showDevTools, + onDevToolsLoad, + devToolsLoaded, + providedFiles, + lintErrors, + lintExtensions, + isExpandable, +}: { + showDevTools: boolean; + devToolsLoaded: boolean; + onDevToolsLoad: () => void; + providedFiles: Array; + lintErrors: Array; + lintExtensions: Array; + isExpandable: boolean; +}) { + const containerRef = useRef(null); + const [isExpanded, setIsExpanded] = useState(false); return ( <>
- + - {isExpandable && ( + {(isExpandable || isExpanded) && (