Last active
May 4, 2021 16:52
-
-
Save khubo/c85c6865ee0b00e9a6f0c006f9e87f86 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| // react hook (from the react-cabal-client) | |
| function useMessage(messageLimit,) { | |
| const {cabalClient} = useContext(CabalProvider); | |
| const [messages, setMessages] = useState(cabalClient.getMessage(messageLimit)); | |
| useEffect() { | |
| cabalClient.subscribe(updateMessages) | |
| return () => cabalClient.unSubscribe(); | |
| , []} | |
| function updateMessage(message) { | |
| // if message is of a certain type update the messages type | |
| if(message.type === "some-message-type") { | |
| setMessages(messages) | |
| } | |
| } | |
| return { | |
| // we could have more here. | |
| messages, | |
| addMessage: cabalClient.addMessage | |
| } | |
| } | |
| // container component in UI | |
| function MessageContainer() { | |
| const { | |
| messages, | |
| addMessage | |
| } = useMessage(100) | |
| return( | |
| <div> | |
| {messages.map(i => <Message message={message}/>)} | |
| </div> | |
| ) | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Client emits different types of updates. So I thought maybe we will have to separate them into different hooks. Like Message hooks, user related hooks etc.