Last active
May 4, 2021 16:52
-
-
Save khubo/c85c6865ee0b00e9a6f0c006f9e87f86 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
// 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> | |
) | |
} |
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.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
this is looking cool! i don't really know that much about hooks, but the sparseness of the code is attractive :~
some typos:
cabalClient.unsubscribe()
(small s, not S)cabalClient.addMessage
, do you meancabal-details.publishMessage()
?