File tree 4 files changed +34
-1
lines changed
4 files changed +34
-1
lines changed Original file line number Diff line number Diff line change
1
+ ---
2
+ " react-mqtt-hooks " : patch
3
+ ---
4
+
5
+ fix caching
Original file line number Diff line number Diff line change 1
1
import { Badge } from "@/components/ui/badge" ;
2
2
import { MqttConnector } from "react-mqtt-hooks" ;
3
3
import ConnectionStatus from "./components/connection-status" ;
4
+ import Demo from "./components/demo" ;
4
5
import MultiTopicsCard from "./components/multi-topics-card" ;
5
6
import SingleTopicCard from "./components/single-topic-card" ;
6
7
import { Button } from "./components/ui/button" ;
@@ -24,7 +25,7 @@ function App() {
24
25
< span className = "font-bold" > { MQTT_URL } </ span >
25
26
</ Badge >
26
27
</ div >
27
- < p className = "" >
28
+ < p >
28
29
You can now publish message to topic
29
30
{ " " }
30
31
< span className = "px-1.5 py-0.5 bg-zinc-200 rounded-lg font-mono font-medium" > chat/1</ span >
@@ -62,6 +63,7 @@ function App() {
62
63
< MultiTopicsCard />
63
64
</ main >
64
65
66
+ < Demo />
65
67
</ div >
66
68
</ MqttConnector >
67
69
) ;
Original file line number Diff line number Diff line change
1
+ import { useState } from "react" ;
2
+ import SingleTopicCard from "./single-topic-card" ;
3
+ import { Button } from "./ui/button" ;
4
+
5
+ export default function Demo ( ) {
6
+ const [ show , setShow ] = useState ( false ) ;
7
+ return (
8
+ < section className = "space-y-2" >
9
+
10
+ < p >
11
+ Component mount/unmount should get the data from cache.
12
+ </ p >
13
+ < Button onClick = { ( ) => setShow ( ! show ) } variant = "outline" size = "lg" >
14
+ { show ? "Hide" : "Show" }
15
+ </ Button >
16
+
17
+ { show && < SingleTopicCard topic = "chat/1" /> }
18
+ </ section >
19
+ ) ;
20
+ }
Original file line number Diff line number Diff line change @@ -21,6 +21,12 @@ export default function useTopic<T = any>(topic: string | null) {
21
21
mqttClient . subscribe ( topic ) ;
22
22
}
23
23
24
+ // when multiple components subscribe to the same topic, return the data from the cache
25
+ const cachedData = cache . getData < T > ( topic ) ;
26
+ if ( cachedData ) {
27
+ setData ( cachedData ) ;
28
+ }
29
+
24
30
const handleDataUpdate = ( newData : T ) => {
25
31
setData ( newData ) ;
26
32
} ;
You can’t perform that action at this time.
0 commit comments