Skip to content

Commit eb15975

Browse files
committed
refactor: basic example with subscribe buttons
1 parent 79f0b01 commit eb15975

File tree

1 file changed

+19
-4
lines changed

1 file changed

+19
-4
lines changed

app/examples/Basic/Child.tsx

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,34 @@
11
import * as React from 'react';
22

3-
import { useMqtt, useSubscribe } from '../../../dist';
3+
import { useMqtt, useSubscribe, useUnsubscribe } from '../../../dist';
44
import { processTopic, actorTopic } from './constants';
55

6+
const topics = [processTopic, actorTopic];
7+
68
export const Child: React.FC = () => {
79
const { status, error } = useMqtt();
810
const subscribe = useSubscribe();
11+
const unsubscribe = useUnsubscribe();
12+
13+
const handleSubscribe = React.useCallback(() => {
14+
if (status === 'connected') subscribe(topics);
15+
}, [status, subscribe]);
916

10-
React.useEffect(() => {
11-
if (status === 'connected') subscribe([processTopic, actorTopic]);
12-
}, [subscribe, status]);
17+
const handleUnsubscribe = React.useCallback(() => {
18+
if (status === 'connected') unsubscribe(topics);
19+
}, [status, unsubscribe]);
1320

1421
return (
1522
<main>
1623
<h1>This is a basic example</h1>
24+
<h2>Topics</h2>
25+
<p>{topics.join(', ')}</p>
26+
<button type="button" onClick={handleSubscribe}>
27+
Inscrever-se nos tópicos
28+
</button>
29+
<button type="button" onClick={handleUnsubscribe}>
30+
Desinscrever-se dos tópicos
31+
</button>
1732
<p>
1833
<b>Mqtt status: </b>
1934
{status}

0 commit comments

Comments
 (0)