mqtt-react-hooks
mqtt-react-hooks copied to clipboard
TypeError: Cannot read properties of undefined (reading 'topic')
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
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