useEffect, useRef and useCallback with 1 project
Published: 2024-06-23 11:47:11

## Password Generator

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vyqpb6sa6nn84m68oqf1.png)

### 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`
4. Install dependencies: npm install
5. Run the development server: npm run dev
6. Configure `tailwind.config.js`
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/eu98p13e5548ygqs1gxl.png)

Now, we gonna start with code.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ujkdhwqvf2uuz6t56sp1.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zekuyybkllqrg6yumf12.png)

## Memoization with useCallback

<h3>`passwordGenerator` Function</h3>

1. <u>Purpose:</u> This function generates a random password based on the specified length and character set.
2. <u>Memoization:</u> 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.

<h3>`copyPasswordToClipboard` Function</h3>

1. <u>Purpose:</u> This function copies the generated password to the clipboard.
2. <u>Memoization: </u>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.

<h3>useEffect Hook</h3>

1. <u>Purpose:</u> This effect runs the passwordGenerator function whenever the dependencies (length, numberAllowed, charAllowed, passwordGenerator) change.
2. <u>Dependency: </u>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.

<h4>Explanation of Memoization</h4>

1. <u>Why Use Memoization?: </u>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. <u>Performance Optimization: </u>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.

<h4>Component Functionality</h4>

1. <u>State Management:</u> The component uses useState to manage length, numberAllowed, charAllowed, and password.
2. <u>Refs:</u> The passwordRef is used to reference the password input field for selecting and copying the password.
3. <u>Event Handling: The</u> 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.