From 7596c74d14b4f050d4cbeb5c7988caff8c3957ef Mon Sep 17 00:00:00 2001 From: Elar Saks Date: Sat, 5 Aug 2023 09:44:32 +0300 Subject: [PATCH 1/2] Fix useId route. --- src/App.js | 8 +++- src/components/AppHeader.jsx | 4 +- src/pages/UseId.jsx | 89 ++++++++++++++++++++++++++++++++++++ 3 files changed, 96 insertions(+), 5 deletions(-) diff --git a/src/App.js b/src/App.js index 5cd7719..502c4f1 100644 --- a/src/App.js +++ b/src/App.js @@ -1,15 +1,16 @@ import app from "./styles/App.module.css"; import { BrowserRouter, Navigate, Route, Routes } from "react-router-dom"; +import AppHeader from "./components/AppHeader"; +import PageNotFound from "./pages/PageNotFound"; import UseCallback from "./pages/UseCallback"; import UseContext from "./pages/UseContext"; import UseEffect from "./pages/UseEffect"; +import UseId from "./pages/UseId"; import UseMemo from "./pages/UseMemo"; import UseReducer from "./pages/UseReducer"; import UseRef from "./pages/UseRef"; -import PageNotFound from "./pages/PageNotFound"; import UseState from "./pages/UseState"; -import AppHeader from "./components/AppHeader"; export default function App() { // Determine the correct base URL based on the environment (local or GitHub Pages) @@ -36,6 +37,9 @@ export default function App() { {/* UseEffect Route */} } /> + {/* UseId Route */} + } /> + {/* UseMemo Route */} } /> diff --git a/src/components/AppHeader.jsx b/src/components/AppHeader.jsx index 7733076..666d437 100644 --- a/src/components/AppHeader.jsx +++ b/src/components/AppHeader.jsx @@ -37,9 +37,7 @@ export default function AppHeader() { useCustom - + diff --git a/src/pages/UseId.jsx b/src/pages/UseId.jsx index e69de29..409c9c3 100644 --- a/src/pages/UseId.jsx +++ b/src/pages/UseId.jsx @@ -0,0 +1,89 @@ +// Import necessary packages, styles, and components +import info from "../styles/Info.module.css"; +import React, { useState, useCallback } from "react"; +import util from "../styles/Util.module.css"; +import Button from "../components/Button"; +import List from "../components/List"; + +// Create a context for theme +export const ThemeContext = React.createContext(); + +// Default component function +export default () => { + // Create state for number and darkTheme using useState hook + const [number, setNumber] = useState(() => 0); + const [darkTheme, setDarktheme] = useState(() => true); + + // Theme styles that change based on darkTheme state + const theme = { + backgroundColor: darkTheme ? "#282c34" : "#fff", + color: darkTheme ? "#fff" : "#282c34", + padding: "10px 20px", + fontSize: "24px", + fontWeight: "bold", + borderRadius: "5px", + border: "2px solid #61DAFB", + marginTop: "20px", + marginLeft: "auto", + marginRight: "auto", + maxWidth: "750px", + }; + + // useCallback hook for creating a memoized version of getItems function + const getItems = useCallback(() => { + return [number, number + 1, number + 2]; + }, [number]); + + //* THIS IS DEBUG - Use this function if you want to see the difference between useCallback and not using it + //* All the the lis items will be re-rendered if you don't use useCallback + // Function that returns an array of numbers + // const getItems = () => { + // return [number, number + 1, number + 2]; + // }; + + return ( + <> + {/* Header and Explanation about useCallback */} +

- useCallback -

+

+ useCallback is a React hook that accepts a function and a + dependencies array as inputs and returns a memoized version of the + function. This hook is useful when passing callbacks to optimized child + components that rely on reference equality to prevent unnecessary + renders. When the dependencies of the useCallback hook + remain the same across re-renders, the same function reference is + returned by the hook. This approach helps in avoiding the re-creation of + functions, thereby saving component re-renders and + improving overall performance. +

+ + {/* Input field to set the number state */} + setNumber(parseInt(e.target.value))} + /> + + {/* Button to toggle the darkTheme state */} +