Skip to content

Instantly share code, notes, and snippets.

@khubo
Last active May 4, 2021 16:52
Show Gist options
  • Save khubo/c85c6865ee0b00e9a6f0c006f9e87f86 to your computer and use it in GitHub Desktop.
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>
)
}
@cblgh
Copy link

cblgh commented May 4, 2021

this is looking cool! i don't really know that much about hooks, but the sparseness of the code is attractive :~

some typos:

  • should be cabalClient.unsubscribe() (small s, not S)
  • instead of cabalClient.addMessage, do you mean cabal-details.publishMessage()?
  • i don't really understand the lines:
  function updateMessage(message) {
    // if message is of a certain type update the messages type
    if(message.type === "some-message-type") {
      setMessages(messages)
    }
  }

@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