Skip to content

Instantly share code, notes, and snippets.

@waqasraza123
Created May 22, 2023 10:07
Show Gist options
  • Save waqasraza123/506b43b51b5258967167816db9255e1a to your computer and use it in GitHub Desktop.
Save waqasraza123/506b43b51b5258967167816db9255e1a to your computer and use it in GitHub Desktop.
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