Created
May 22, 2023 10:07
-
-
Save waqasraza123/506b43b51b5258967167816db9255e1a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { useState, useEffect } from 'react'; | |
const ExampleComponent = () => { | |
const [data, setData] = useState(null); | |
const [count, setCount] = useState(0); | |
useEffect(() => { | |
// Fetching data from an API | |
fetch('https://api.example.com/data') | |
.then(response => response.json()) | |
.then(data => setData(data)); | |
// Updating the document title | |
document.title = `Count: ${count}`; | |
// Adding an event listener | |
const handleClick = () => { | |
setCount(prevCount => prevCount + 1); | |
}; | |
document.addEventListener('click', handleClick); | |
// Subscription management | |
const subscription = externalService.subscribe(() => { | |
// Do something with the received data | |
}); | |
// Cleanup operation when the component unmounts | |
return () => { | |
document.removeEventListener('click', handleClick); | |
subscription.unsubscribe(); | |
}; | |
}, [count]); | |
// Render the component | |
return ( | |
<div> | |
<p>Data: {data}</p> | |
<p>Count: {count}</p> | |
{/* ... */} | |
</div> | |
); | |
}; | |
export default ExampleComponent; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment