useeffect in react in javascript

index.tsx
import React, { useState, useEffect } from 'react';

const MyComponent = () => {
    const [count, setCount] = useState(0);

    // useEffect hook to perform side effects in a functional component
    useEffect(() => {
        // code inside this block will execute after every render
        document.title = `You clicked ${count} times`;
    }, [count]); // dependency array ensures it only runs when count changes

    return (
        <div>
            <p>You clicked {count} times</p>
            <button onClick={() => setCount(count + 1)}>Click me</button>
        </div>
    );
};

export default MyComponent;
618 chars
21 lines

related categories

gistlibby LogSnag