Skip to content

Instantly share code, notes, and snippets.

@juunegreiros
Created October 12, 2021 20:41
Show Gist options
  • Save juunegreiros/01e4194a5691d39614c514c0008e01cb to your computer and use it in GitHub Desktop.
Save juunegreiros/01e4194a5691d39614c514c0008e01cb to your computer and use it in GitHub Desktop.
Monitoria 8
// depende da ordem em que é chamado
// começa com use e pode usar outros hooks
import { useState, useEffect } from 'react';
function useFriendStatus(friendID) {
const [isOnline, setIsOnline] = useState(null);
useEffect(() => {
function handleStatusChange(status) {
setIsOnline(status.isOnline);
}
ChatAPI.subscribeToFriendStatus(friendID, handleStatusChange);
return () => {
ChatAPI.unsubscribeFromFriendStatus(friendID, handleStatusChange);
};
});
return isOnline;
}
import React, { useState, useEffect } from 'react';
function Exemplo() {
const [count, setCount] = useState(0);
// Similar ao componentDidMount e componentDidUpdate:
useEffect(() => {
// Atualiza o titulo do documento usando a API do browser
document.title = `Você clicou ${count} vezes`;
});
return (
<div>
<p>Você clicou {count} vezes</p>
<button onClick={() => setCount(count + 1)}>
Clique aqui
</button>
</div>
);
}
const [count, setCount] = useState(0);
return (
<div>
<p>You clicked {count} times</p>
<button onClick={() => setCount(count + 1)}>
Click me
</button>
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment