Skip to content

Instantly share code, notes, and snippets.

@luistak
Created January 27, 2021 15:45
Show Gist options
  • Save luistak/ff2bf4bc7a5567bbbb06044e8dcd5ba3 to your computer and use it in GitHub Desktop.
Save luistak/ff2bf4bc7a5567bbbb06044e8dcd5ba3 to your computer and use it in GitHub Desktop.
import { Observable } from 'windowed-observable';
const observable = new Observable('messages');
function App() {
const [messages, setMessages] = useState([]);
const handleNewMessage = (newMessage) => {
setMessages((currentMessages) => currentMessages.concat(newMessage));
};
useEffect(() => {
observable.subscribe(handleNewMessage);
return () => {
observable.unsubscribe(handleNewMessage)
}
}, [handleNewMessage]);
...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment