Skip to content

Instantly share code, notes, and snippets.

@khubo
Last active May 4, 2021 16:52
Show Gist options
  • Select an option

  • Save khubo/c85c6865ee0b00e9a6f0c006f9e87f86 to your computer and use it in GitHub Desktop.

Select an option

Save khubo/c85c6865ee0b00e9a6f0c006f9e87f86 to your computer and use it in GitHub Desktop.
// 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>
)
}
@khubo
Copy link
Author

khubo commented May 4, 2021

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