mqtt-react-hooks icon indicating copy to clipboard operation
mqtt-react-hooks copied to clipboard

TypeError: Cannot read properties of undefined (reading 'topic')

Open kitar-kks opened this issue 3 years ago • 1 comments

I got a error from use Subscription example ( TypeError: Cannot read properties of undefined (reading 'topic') )

`import React from 'react';

import { useSubscription } from 'mqtt-react-hooks';

export default function Status() { /* Message structure:

  • topic: string
  • message: string */ const { message } = useSubscription([ 'room/esp32/testing', 'room/esp32/light', ]);

return ( <> <div style={{ display: 'flex', flexDirection: 'column' }}> {topic:${message.topic} - message: ${message.message}} </> ); }` How can I fix it

kitar-kks avatar Dec 17 '21 04:12 kitar-kks

Just try {topic:${message?.topic} - message: ${message?.message}} It will print : "topic: undefined - message: undefined" if your message object is empty and it's going to be re-rendered each time a message arrive without error

thomasklingel avatar Jan 01 '22 16:01 thomasklingel