@unitedstatespowersquadrons/components 1.2.9 → 1.2.10-pre.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -13,7 +13,7 @@ const SubComponent = () => {
13
13
  };
14
14
 
15
15
  const sendUpstreamMessage = () => {
16
- channel.send({ message: "upstream" });
16
+ if (channel) channel.send({ message: "upstream" });
17
17
  };
18
18
 
19
19
  return(
@@ -1,7 +1,7 @@
1
- import React, { createContext, useContext, useMemo } from "react";
1
+ import React, { createContext, useContext, useEffect, useMemo, useRef } from "react";
2
2
  import { Draft, produce } from "immer";
3
3
  import { useImmerReducer } from "use-immer";
4
- import { Cable, createConsumer } from "@rails/actioncable";
4
+ import { Cable, Channel, createConsumer } from "@rails/actioncable";
5
5
  import { CableAction, DispatchFunc, ReducerHandler } from "./types";
6
6
 
7
7
  interface InitialAppProps<AppState> {
@@ -63,13 +63,16 @@ export default function buildReducer<AppState, AppAction>(
63
63
 
64
64
  function useAppCableSubscription(dispatch: DispatchFunc<CableAction>, channel: string) {
65
65
  const appCable = useAppCable();
66
+ const channelRef = useRef<Channel>(null);
66
67
 
67
- return(
68
- appCable.subscriptions.create(
68
+ useEffect(() => {
69
+ channelRef.current = appCable.subscriptions.create(
69
70
  { channel },
70
71
  { received: (data: object) => dispatch({ data, type: "receivedCable" } as CableAction) }
71
- )
72
- );
72
+ );
73
+ }, [appCable, channel, dispatch]);
74
+
75
+ return channelRef.current;
73
76
  }
74
77
 
75
78
  function useAppDispatch() {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@unitedstatespowersquadrons/components",
3
- "version": "1.2.9",
3
+ "version": "1.2.10-pre.2",
4
4
  "description": "USPS shared React components library",
5
5
  "main": "index.tsx",
6
6
  "scripts": {