Skip to content
Discussion options

You must be logged in to vote

These are untested, but I would think one of these options would work:

// option 1
// keep a ref to the client so React saves a copy between renders
const clientRef = React.useRef(null)

useEffect(() => {  
  // access client vis clientRef.current
  console.dir(${clientRef.current});
  if (clientRef.current) {
    console.log(`client: ${clientRef.current}`);
    clientRef.current = mqtt.connect(mqttUri, options);
    clientRef.current.subscribe('test');
    clientRef.current.on('message', (message) => { setMessages(messages.concat(message.toString())); });
  }

  return () => {
    // always clean up the effect if clientRef.current has a value
    if (clientRef.current) {
      clientRef.c…

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@smaktacular
Comment options

Answer selected by jamesmosier
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Help
Labels
None yet
2 participants