diff --git a/src/App.tsx b/src/App.tsx index e74b4aa..9683a7a 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -1,4 +1,4 @@ -import { useEffect, useRef, useState } from "react"; +import { useCallback, useEffect, useRef, useState } from "react"; import { Box, @@ -37,33 +37,36 @@ function App() { ); const isHorizontalLayout = userLayout === "horizontal" || isMuiMdScreen; - const handleRunClick = () => { + const handleRunClick = useCallback(() => { setStdout([]); setStderr([]); momonga_run(srcRef.current); // NOTE: In order to run on Worker, it is necessary to change the way of passing its output data to main thread. - }; + }, []); - const handleSrcChange = (src: string) => { + const handleSrcChange = useCallback((src: string) => { srcRef.current = src; if (!isWasmIntitializedRef.current) return; setIsParseError(is_momonga_parse_error(src)); - }; + }, []); - const handleLayoutClick = () => { + const handleLayoutClick = useCallback(() => { setUserLayout((prev) => prev === "horizontal" ? "vertical" : "horizontal", ); - }; + }, []); - const handleSnippetChange = (event: SelectChangeEvent) => { - const snippet = snippets.find( - (snippet) => snippet.key === event.target.value, - ); - if (snippet) { - setSnippetKey(snippet.key); - } - }; + const handleSnippetChange = useCallback( + (event: SelectChangeEvent) => { + const snippet = snippets.find( + (snippet) => snippet.key === event.target.value, + ); + if (snippet) { + setSnippetKey(snippet.key); + } + }, + [], + ); useEffect(() => { (async () => { diff --git a/src/components/Editor.tsx b/src/components/Editor.tsx index 0d5c68c..0504b7a 100644 --- a/src/components/Editor.tsx +++ b/src/components/Editor.tsx @@ -1,4 +1,4 @@ -import React, { MutableRefObject, useEffect, useRef, useState } from "react"; +import { MutableRefObject, memo, useEffect, useRef, useState } from "react"; import { Box } from "@mui/material"; import * as monaco from "monaco-editor/esm/vs/editor/editor.api"; @@ -103,7 +103,7 @@ type Props = { onSrcChange: (src: string) => void; }; -export const Editor = React.memo( +export const Editor = memo( ({ isParseError, srcRef, snippetKey, onSrcChange }: Props) => { const [editor, setEditor] = useState(null); diff --git a/src/components/Header.tsx b/src/components/Header.tsx index 0150ec7..eaf86a9 100644 --- a/src/components/Header.tsx +++ b/src/components/Header.tsx @@ -1,4 +1,4 @@ -import { useContext } from "react"; +import { memo, useContext } from "react"; import { AppBar, @@ -29,83 +29,85 @@ type Props = { onMainLayoutClick: () => void; }; -export const Header = ({ - isMuiMdScreen, - isHorizontalLayout, - snippetKey, - onMainLayoutClick, - onRunClick, - onSnippetChange, -}: Props) => { - const { mode, toggleMode } = useContext(ThemeContext); - return ( - - - - - - - Code Snippets - - - - - - + + + Code Snippets + + + + + - Grammar - - {!isMuiMdScreen && ( - - {isHorizontalLayout ? ( - - ) : ( - - )} + + {!isMuiMdScreen && ( + + {isHorizontalLayout ? ( + + ) : ( + + )} + + )} + + {mode === "light" ? : } - )} - - {mode === "light" ? : } - - - - - ); -}; + + + + ); + }, +); diff --git a/src/components/Output.tsx b/src/components/Output.tsx index d0b0820..2887cd7 100644 --- a/src/components/Output.tsx +++ b/src/components/Output.tsx @@ -1,3 +1,5 @@ +import { memo } from "react"; + import { Box, Divider } from "@mui/material"; import { Stderr, Stdout } from "@/types/types"; @@ -7,7 +9,7 @@ type Props = { stderr: Stderr; }; -export const Output = ({ stdout, stderr }: Props) => { +export const Output = memo(({ stdout, stderr }: Props) => { return ( { ); -}; +});