@xstate/react 4.0.3 → 4.1.1

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.
package/README.md CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  This package contains utilities for using [XState](https://github.com/statelyai/xstate) with [React](https://github.com/facebook/react/).
4
4
 
5
- - [Read the full documentation in the XState docs](https://xstate.js.org/docs/packages/xstate-react/).
5
+ - [Read the full documentation in the XState docs](https://stately.ai/docs/xstate-react).
6
6
  - [Read our contribution guidelines](https://github.com/statelyai/xstate/blob/main/CONTRIBUTING.md).
7
7
 
8
8
  ## Quick start
@@ -15,7 +15,7 @@ npm i xstate @xstate/react
15
15
 
16
16
  2. Import the `useMachine` hook:
17
17
 
18
- ```js
18
+ ```tsx
19
19
  import { useMachine } from '@xstate/react';
20
20
  import { createMachine } from 'xstate';
21
21
 
@@ -36,7 +36,7 @@ export const Toggler = () => {
36
36
  const [state, send] = useMachine(toggleMachine);
37
37
 
38
38
  return (
39
- <button onClick={() => send('TOGGLE')}>
39
+ <button onClick={() => send({ type: 'TOGGLE' })}>
40
40
  {state.value === 'inactive'
41
41
  ? 'Click to activate'
42
42
  : 'Active! Click to deactivate'}
@@ -1,2 +1,4 @@
1
- import { ActorRef, SnapshotFrom } from 'xstate';
2
- export declare function useSelector<TActor extends ActorRef<any, any>, T>(actor: TActor, selector: (emitted: SnapshotFrom<TActor>) => T, compare?: (a: T, b: T) => boolean): T;
1
+ import { AnyActorRef } from 'xstate';
2
+ export declare function useSelector<TActor extends Pick<AnyActorRef, 'subscribe' | 'getSnapshot'> | undefined, T>(actor: TActor, selector: (snapshot: TActor extends {
3
+ getSnapshot(): infer TSnapshot;
4
+ } ? TSnapshot : undefined) => T, compare?: (a: T, b: T) => boolean): T;
@@ -131,12 +131,15 @@ function defaultCompare(a, b) {
131
131
  }
132
132
  function useSelector(actor, selector, compare = defaultCompare) {
133
133
  const subscribe = React.useCallback(handleStoreChange => {
134
+ if (!actor) {
135
+ return () => {};
136
+ }
134
137
  const {
135
138
  unsubscribe
136
139
  } = actor.subscribe(handleStoreChange);
137
140
  return unsubscribe;
138
141
  }, [actor]);
139
- const boundGetSnapshot = React.useCallback(() => actor.getSnapshot(), [actor]);
142
+ const boundGetSnapshot = React.useCallback(() => actor?.getSnapshot(), [actor]);
140
143
  const selectedSnapshot = withSelector.useSyncExternalStoreWithSelector(subscribe, boundGetSnapshot, boundGetSnapshot, selector, compare);
141
144
  return selectedSnapshot;
142
145
  }
@@ -134,12 +134,15 @@ function defaultCompare(a, b) {
134
134
  }
135
135
  function useSelector(actor, selector, compare = defaultCompare) {
136
136
  const subscribe = React.useCallback(handleStoreChange => {
137
+ if (!actor) {
138
+ return () => {};
139
+ }
137
140
  const {
138
141
  unsubscribe
139
142
  } = actor.subscribe(handleStoreChange);
140
143
  return unsubscribe;
141
144
  }, [actor]);
142
- const boundGetSnapshot = React.useCallback(() => actor.getSnapshot(), [actor]);
145
+ const boundGetSnapshot = React.useCallback(() => actor?.getSnapshot(), [actor]);
143
146
  const selectedSnapshot = withSelector.useSyncExternalStoreWithSelector(subscribe, boundGetSnapshot, boundGetSnapshot, selector, compare);
144
147
  return selectedSnapshot;
145
148
  }
@@ -108,12 +108,15 @@ function defaultCompare(a, b) {
108
108
  }
109
109
  function useSelector(actor, selector, compare = defaultCompare) {
110
110
  const subscribe = useCallback(handleStoreChange => {
111
+ if (!actor) {
112
+ return () => {};
113
+ }
111
114
  const {
112
115
  unsubscribe
113
116
  } = actor.subscribe(handleStoreChange);
114
117
  return unsubscribe;
115
118
  }, [actor]);
116
- const boundGetSnapshot = useCallback(() => actor.getSnapshot(), [actor]);
119
+ const boundGetSnapshot = useCallback(() => actor?.getSnapshot(), [actor]);
117
120
  const selectedSnapshot = useSyncExternalStoreWithSelector(subscribe, boundGetSnapshot, boundGetSnapshot, selector, compare);
118
121
  return selectedSnapshot;
119
122
  }
@@ -105,12 +105,15 @@ function defaultCompare(a, b) {
105
105
  }
106
106
  function useSelector(actor, selector, compare = defaultCompare) {
107
107
  const subscribe = useCallback(handleStoreChange => {
108
+ if (!actor) {
109
+ return () => {};
110
+ }
108
111
  const {
109
112
  unsubscribe
110
113
  } = actor.subscribe(handleStoreChange);
111
114
  return unsubscribe;
112
115
  }, [actor]);
113
- const boundGetSnapshot = useCallback(() => actor.getSnapshot(), [actor]);
116
+ const boundGetSnapshot = useCallback(() => actor?.getSnapshot(), [actor]);
114
117
  const selectedSnapshot = useSyncExternalStoreWithSelector(subscribe, boundGetSnapshot, boundGetSnapshot, selector, compare);
115
118
  return selectedSnapshot;
116
119
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@xstate/react",
3
- "version": "4.0.3",
3
+ "version": "4.1.1",
4
4
  "description": "XState tools for React",
5
5
  "keywords": [
6
6
  "state",
@@ -55,7 +55,7 @@
55
55
  },
56
56
  "peerDependencies": {
57
57
  "react": "^16.8.0 || ^17.0.0 || ^18.0.0",
58
- "xstate": "^5.5.2"
58
+ "xstate": "^5.11.0"
59
59
  },
60
60
  "peerDependenciesMeta": {
61
61
  "xstate": {
@@ -64,10 +64,10 @@
64
64
  },
65
65
  "dependencies": {
66
66
  "use-isomorphic-layout-effect": "^1.1.2",
67
- "use-sync-external-store": "^1.0.0"
67
+ "use-sync-external-store": "^1.2.0"
68
68
  },
69
69
  "devDependencies": {
70
- "@testing-library/react": "^13.4.0",
70
+ "@testing-library/react": "^14.2.1",
71
71
  "@types/jsdom": "^12.2.3",
72
72
  "@types/react": "^17.0.43",
73
73
  "@types/react-dom": "^17.0.14",
@@ -76,6 +76,6 @@
76
76
  "jsdom-global": "^3.0.2",
77
77
  "react": "^18.0.0",
78
78
  "react-dom": "^18.0.0",
79
- "xstate": "5.5.2"
79
+ "xstate": "5.11.0"
80
80
  }
81
81
  }