Created
January 27, 2021 15:45
-
-
Save luistak/ff2bf4bc7a5567bbbb06044e8dcd5ba3 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 { 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