DEV Community

Geetika Bajpai
Geetika Bajpai

Posted on

useEffect, useRef and useCallback with 1 project

Password Generator

Image description

Thoughts

  1. There is a method currently running that generates random text by default.
  2. This method needs to execute repeatedly because any changes in the input parameters, such as length, or toggling checkboxes for numbers and characters, result in new random text being generated.
  3. Since this method runs frequently, we should consider optimization techniques. We can leverage memoization, which is inherently supported by React hooks, to optimize these methods effectively.
  4. The copy button should be configured to specifically target and copy only the text within the designated text box.

Don't panic read the full article you will understand and this article is written by watching videos of Sir Hitesh Choudhary in YT and reading documentation.

  1. Create a Vite project: npm create vite@latest
  2. Navigate to the project directory: cd my-vite-project
  3. Install npm install -D tailwindcss autoprefixer
  4. Install npx tailwindcss init -p
  5. Install dependencies: npm install
  6. Run the development server: npm run dev
  7. Configure tailwind.config.js Image description

Now, we gonna start with code.

Image description

Image description

Memoization with useCallback

`passwordGenerator` Function

  1. Purpose: This function generates a random password based on the specified length and character set.
  2. Memoization: The useCallback hook memoizes the passwordGenerator function, ensuring that it only gets recreated if any of the dependencies (length, numberAllowed, charAllowed, setPassword) change. This helps in avoiding unnecessary function re-creations and optimizes performance.

`copyPasswordToClipboard` Function

  1. Purpose: This function copies the generated password to the clipboard.
  2. Memoization: The useCallback hook memoizes the copyPasswordToClipboard function, ensuring it only gets recreated if the password dependency changes. This optimizes performance by preventing unnecessary re-creations of the function.

useEffect Hook

  1. Purpose: This effect runs the passwordGenerator function whenever the dependencies (length, numberAllowed, charAllowed, passwordGenerator) change.
  2. Dependency: The passwordGenerator function is memoized, which means the effect will only rerun when the actual logic inside the generator needs to change, optimizing the component’s rendering.

Explanation of Memoization

  1. Why Use Memoization?: Memoization helps to avoid unnecessary recalculations or re-creations of functions, especially useful in functional components where functions might be redefined on each render.
  2. Performance Optimization: By using useCallback, the component ensures that passwordGenerator and copyPasswordToClipboard are only recreated when their respective dependencies change. This can reduce the rendering overhead and improve the overall performance of the component.

Component Functionality

  1. State Management: The component uses useState to manage length, numberAllowed, charAllowed, and password.
  2. Refs: The passwordRef is used to reference the password input field for selecting and copying the password.
  3. Event Handling: The component handles various user interactions like changing the password length, toggling the inclusion of numbers and special characters, and copying the password to the clipboard.

Overall, the use of useCallback in this component ensures that functions are only recreated when necessary, optimizing the performance by avoiding unnecessary re-renders and re-creations.

Top comments (0)