Posts

Showing posts from June, 2025

Memory

 import React, { useState, useCallback } from 'react'; function ParentComponent() {   const [clickCount, setClickCount] = useState(0); // Changes on every button click   const [otherValue, setOtherValue] = useState(0); // Changes only when its button is clicked   // This function will always be recreated because 'clickCount' changes on every click   const incrementClickCount = useCallback(() => {     console.log('incrementClickCount called. Current clickCount:', clickCount);     setClickCount(prevCount => prevCount + 1);   }, [clickCount]); // Dependency: clickCount   // This function will only be recreated if 'otherValue' changes   // It demonstrates how useCallback *prevents* recreation when 'clickCount' changes   const updateOtherValue = useCallback(() => {     console.log('updateOtherValue called. Current otherValue:', otherValue);     setOtherValue(prevValue => prevValue + 1);   },...

Usecallback 3

 The text in the image describes the useCallback hook in React: useCallback The useCallback hook is used to memoize functions in functional components. Its main purpose is to prevent the unnecessary re-creation of functions on every render of a component. By wrapping a function with useCallback, React caches the function instance and only returns a new instance if one of its specified dependencies has changed. import React, { useState, useCallback } from 'react'; function App() {   const [count, setCount] = useState(0);   // Function that is recreated on every render   const incrementWithoutCallback = () => {     setCount(count + 1);   };   // Function that is memoized; only recreated if 'count' changes   const incrementWithCallback =  useCallback(() => {     setCount(prevCount => prevCount + 1);   }, [count]);   return (     <div>       <p>Count: {count}</p> ...

UseRef 2

 UseRef:- The useRef hook is primarily used for accessing and interacting with DOM elements. The advantage of the useRef hook is that it remains consistent across renders and doesn't trigger re-renders when its value changes import React, { useRef } from 'react'; function MyComponent() {   const inputRef = useRef(null);   const focusInput = () => {     if (inputRef.current) {       inputRef.current.focus();     }   };   return (     <div>       <h2>Using useRef Example</h2>       <input type="text" ref={inputRef} placeholder="Type something..." />       <button onClick={focusInput}>Focus Input</button>     </div>   ); } export d efault MyComponent;

Lifecycle hooks and useEffect-1

 Here's the corrected document: Lifecycle Methods: When we perform tasks like:  * Component loads  * Component updates  * Component unmounts Class-based Components: We have:  * componentDidMount  * componentDidUpdate  * componentWillUnmount Functional-based Components: We use the useEffect hook. We need to pass dependencies:  * If passing an empty dependency array ([]), it will work as componentDidMount.  * If passing dependencies, it works as componentDidUpdate.  * If we pass a return keyword, it works as componentWillUnmount. Definition of useEffect: useEffect is React's solution for handling side effects in functional components. It allows your component to perform actions that go beyond just rendering UI. This means that the primary job of a React component is to display something on the screen. What are Side Effects? Actions that go beyond just rendering UI are called side effects. Examples of Side Effects:  * Fetching data fro...