@xstate/react 4.0.2 → 4.1.0
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 +0 -8
- package/dist/declarations/src/createActorContext.d.ts +3 -3
- package/dist/declarations/src/useActor.d.ts +2 -2
- package/dist/declarations/src/useActorRef.d.ts +3 -3
- package/dist/declarations/src/useMachine.d.ts +2 -6
- package/dist/declarations/src/useSelector.d.ts +1 -1
- package/dist/xstate-react.cjs.js +11 -5
- package/dist/xstate-react.development.cjs.js +11 -5
- package/dist/xstate-react.development.esm.js +11 -5
- package/dist/xstate-react.esm.js +11 -5
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -13,14 +13,6 @@ This package contains utilities for using [XState](https://github.com/statelyai/
|
|
|
13
13
|
npm i xstate @xstate/react
|
|
14
14
|
```
|
|
15
15
|
|
|
16
|
-
**Via CDN**
|
|
17
|
-
|
|
18
|
-
```html
|
|
19
|
-
<script src="https://unpkg.com/@xstate/react/dist/xstate-react.umd.min.js"></script>
|
|
20
|
-
```
|
|
21
|
-
|
|
22
|
-
By using the global variable `XStateReact`
|
|
23
|
-
|
|
24
16
|
2. Import the `useMachine` hook:
|
|
25
17
|
|
|
26
18
|
```js
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import * as React from 'react';
|
|
2
|
-
import {
|
|
3
|
-
export declare function createActorContext<TLogic extends AnyActorLogic>(actorLogic: TLogic,
|
|
2
|
+
import { Actor, ActorOptions, AnyActorLogic, SnapshotFrom } from 'xstate';
|
|
3
|
+
export declare function createActorContext<TLogic extends AnyActorLogic>(actorLogic: TLogic, actorOptions?: ActorOptions<TLogic>): {
|
|
4
4
|
useSelector: <T>(selector: (snapshot: SnapshotFrom<TLogic>) => T, compare?: (a: T, b: T) => boolean) => T;
|
|
5
|
-
useActorRef: () =>
|
|
5
|
+
useActorRef: () => Actor<TLogic>;
|
|
6
6
|
Provider: (props: {
|
|
7
7
|
children: React.ReactNode;
|
|
8
8
|
options?: ActorOptions<TLogic>;
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import {
|
|
2
|
-
export declare function useActor<TLogic extends AnyActorLogic>(logic: TLogic, options?: ActorOptions<TLogic>): [SnapshotFrom<TLogic>,
|
|
1
|
+
import { Actor, ActorOptions, AnyActorLogic, SnapshotFrom } from 'xstate';
|
|
2
|
+
export declare function useActor<TLogic extends AnyActorLogic>(logic: TLogic, options?: ActorOptions<TLogic>): [SnapshotFrom<TLogic>, Actor<TLogic>['send'], Actor<TLogic>];
|
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import {
|
|
2
|
-
export declare function useIdleActorRef(logic:
|
|
3
|
-
export declare function useActorRef<TLogic extends AnyActorLogic>(machine: TLogic, options?: ActorOptions<TLogic>, observerOrListener?: Observer<SnapshotFrom<TLogic>> | ((value: SnapshotFrom<TLogic>) => void)):
|
|
1
|
+
import { Actor, ActorOptions, AnyActorLogic, Observer, SnapshotFrom } from 'xstate';
|
|
2
|
+
export declare function useIdleActorRef<TLogic extends AnyActorLogic>(logic: TLogic, options: Partial<ActorOptions<TLogic>>): Actor<TLogic>;
|
|
3
|
+
export declare function useActorRef<TLogic extends AnyActorLogic>(machine: TLogic, options?: ActorOptions<TLogic>, observerOrListener?: Observer<SnapshotFrom<TLogic>> | ((value: SnapshotFrom<TLogic>) => void)): Actor<TLogic>;
|
|
@@ -1,9 +1,5 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { Actor, ActorOptions, AnyStateMachine, StateFrom } from 'xstate';
|
|
2
2
|
/**
|
|
3
3
|
* @alias useActor
|
|
4
4
|
*/
|
|
5
|
-
export declare function useMachine<TMachine extends AnyStateMachine>(machine: TMachine, options?: ActorOptions<TMachine>): [
|
|
6
|
-
StateFrom<TMachine>,
|
|
7
|
-
ActorRefFrom<TMachine>['send'],
|
|
8
|
-
ActorRefFrom<TMachine>
|
|
9
|
-
];
|
|
5
|
+
export declare function useMachine<TMachine extends AnyStateMachine>(machine: TMachine, options?: ActorOptions<TMachine>): [StateFrom<TMachine>, Actor<TMachine>['send'], Actor<TMachine>];
|
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
import { ActorRef, SnapshotFrom } from 'xstate';
|
|
2
|
-
export declare function useSelector<TActor extends ActorRef<any, any
|
|
2
|
+
export declare function useSelector<TActor extends ActorRef<any, any> | undefined, T>(actor: TActor, selector: (emitted: TActor extends ActorRef<any, any> ? SnapshotFrom<TActor> : undefined) => T, compare?: (a: T, b: T) => boolean): T;
|
package/dist/xstate-react.cjs.js
CHANGED
|
@@ -4,8 +4,8 @@ Object.defineProperty(exports, '__esModule', { value: true });
|
|
|
4
4
|
|
|
5
5
|
var React = require('react');
|
|
6
6
|
var shim = require('use-sync-external-store/shim');
|
|
7
|
-
var xstate = require('xstate');
|
|
8
7
|
var useIsomorphicLayoutEffect = require('use-isomorphic-layout-effect');
|
|
8
|
+
var xstate = require('xstate');
|
|
9
9
|
var withSelector = require('use-sync-external-store/shim/with-selector');
|
|
10
10
|
|
|
11
11
|
function _interopDefault (e) { return e && e.__esModule ? e : { 'default': e }; }
|
|
@@ -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
|
|
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
|
}
|
|
@@ -165,19 +168,22 @@ function shallowEqual(objA, objB) {
|
|
|
165
168
|
return true;
|
|
166
169
|
}
|
|
167
170
|
|
|
168
|
-
function createActorContext(actorLogic,
|
|
171
|
+
function createActorContext(actorLogic, actorOptions) {
|
|
169
172
|
const ReactContext = /*#__PURE__*/React__namespace.createContext(null);
|
|
170
173
|
const OriginalProvider = ReactContext.Provider;
|
|
171
174
|
function Provider({
|
|
172
175
|
children,
|
|
173
176
|
logic: providedLogic = actorLogic,
|
|
174
177
|
machine,
|
|
175
|
-
options: providedOptions
|
|
178
|
+
options: providedOptions
|
|
176
179
|
}) {
|
|
177
180
|
if (machine) {
|
|
178
181
|
throw new Error(`The "machine" prop has been deprecated. Please use "logic" instead.`);
|
|
179
182
|
}
|
|
180
|
-
const actor = useActorRef(providedLogic,
|
|
183
|
+
const actor = useActorRef(providedLogic, {
|
|
184
|
+
...actorOptions,
|
|
185
|
+
...providedOptions
|
|
186
|
+
});
|
|
181
187
|
return /*#__PURE__*/React__namespace.createElement(OriginalProvider, {
|
|
182
188
|
value: actor,
|
|
183
189
|
children
|
|
@@ -4,8 +4,8 @@ Object.defineProperty(exports, '__esModule', { value: true });
|
|
|
4
4
|
|
|
5
5
|
var React = require('react');
|
|
6
6
|
var shim = require('use-sync-external-store/shim');
|
|
7
|
-
var xstate = require('xstate');
|
|
8
7
|
var useIsomorphicLayoutEffect = require('use-isomorphic-layout-effect');
|
|
8
|
+
var xstate = require('xstate');
|
|
9
9
|
var withSelector = require('use-sync-external-store/shim/with-selector');
|
|
10
10
|
|
|
11
11
|
function _interopDefault (e) { return e && e.__esModule ? e : { 'default': e }; }
|
|
@@ -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
|
|
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
|
}
|
|
@@ -168,19 +171,22 @@ function shallowEqual(objA, objB) {
|
|
|
168
171
|
return true;
|
|
169
172
|
}
|
|
170
173
|
|
|
171
|
-
function createActorContext(actorLogic,
|
|
174
|
+
function createActorContext(actorLogic, actorOptions) {
|
|
172
175
|
const ReactContext = /*#__PURE__*/React__namespace.createContext(null);
|
|
173
176
|
const OriginalProvider = ReactContext.Provider;
|
|
174
177
|
function Provider({
|
|
175
178
|
children,
|
|
176
179
|
logic: providedLogic = actorLogic,
|
|
177
180
|
machine,
|
|
178
|
-
options: providedOptions
|
|
181
|
+
options: providedOptions
|
|
179
182
|
}) {
|
|
180
183
|
if (machine) {
|
|
181
184
|
throw new Error(`The "machine" prop has been deprecated. Please use "logic" instead.`);
|
|
182
185
|
}
|
|
183
|
-
const actor = useActorRef(providedLogic,
|
|
186
|
+
const actor = useActorRef(providedLogic, {
|
|
187
|
+
...actorOptions,
|
|
188
|
+
...providedOptions
|
|
189
|
+
});
|
|
184
190
|
return /*#__PURE__*/React__namespace.createElement(OriginalProvider, {
|
|
185
191
|
value: actor,
|
|
186
192
|
children
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import * as React from 'react';
|
|
2
2
|
import { useEffect, useState, useCallback } from 'react';
|
|
3
3
|
import { useSyncExternalStore } from 'use-sync-external-store/shim';
|
|
4
|
-
import { toObserver, createActor } from 'xstate';
|
|
5
4
|
import useIsomorphicLayoutEffect from 'use-isomorphic-layout-effect';
|
|
5
|
+
import { toObserver, createActor } from 'xstate';
|
|
6
6
|
import { useSyncExternalStoreWithSelector } from 'use-sync-external-store/shim/with-selector';
|
|
7
7
|
|
|
8
8
|
const forEachActor = (actorRef, callback) => {
|
|
@@ -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
|
|
119
|
+
const boundGetSnapshot = useCallback(() => actor?.getSnapshot(), [actor]);
|
|
117
120
|
const selectedSnapshot = useSyncExternalStoreWithSelector(subscribe, boundGetSnapshot, boundGetSnapshot, selector, compare);
|
|
118
121
|
return selectedSnapshot;
|
|
119
122
|
}
|
|
@@ -142,19 +145,22 @@ function shallowEqual(objA, objB) {
|
|
|
142
145
|
return true;
|
|
143
146
|
}
|
|
144
147
|
|
|
145
|
-
function createActorContext(actorLogic,
|
|
148
|
+
function createActorContext(actorLogic, actorOptions) {
|
|
146
149
|
const ReactContext = /*#__PURE__*/React.createContext(null);
|
|
147
150
|
const OriginalProvider = ReactContext.Provider;
|
|
148
151
|
function Provider({
|
|
149
152
|
children,
|
|
150
153
|
logic: providedLogic = actorLogic,
|
|
151
154
|
machine,
|
|
152
|
-
options: providedOptions
|
|
155
|
+
options: providedOptions
|
|
153
156
|
}) {
|
|
154
157
|
if (machine) {
|
|
155
158
|
throw new Error(`The "machine" prop has been deprecated. Please use "logic" instead.`);
|
|
156
159
|
}
|
|
157
|
-
const actor = useActorRef(providedLogic,
|
|
160
|
+
const actor = useActorRef(providedLogic, {
|
|
161
|
+
...actorOptions,
|
|
162
|
+
...providedOptions
|
|
163
|
+
});
|
|
158
164
|
return /*#__PURE__*/React.createElement(OriginalProvider, {
|
|
159
165
|
value: actor,
|
|
160
166
|
children
|
package/dist/xstate-react.esm.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import * as React from 'react';
|
|
2
2
|
import { useEffect, useState, useCallback } from 'react';
|
|
3
3
|
import { useSyncExternalStore } from 'use-sync-external-store/shim';
|
|
4
|
-
import { toObserver, createActor } from 'xstate';
|
|
5
4
|
import useIsomorphicLayoutEffect from 'use-isomorphic-layout-effect';
|
|
5
|
+
import { toObserver, createActor } from 'xstate';
|
|
6
6
|
import { useSyncExternalStoreWithSelector } from 'use-sync-external-store/shim/with-selector';
|
|
7
7
|
|
|
8
8
|
const forEachActor = (actorRef, callback) => {
|
|
@@ -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
|
|
116
|
+
const boundGetSnapshot = useCallback(() => actor?.getSnapshot(), [actor]);
|
|
114
117
|
const selectedSnapshot = useSyncExternalStoreWithSelector(subscribe, boundGetSnapshot, boundGetSnapshot, selector, compare);
|
|
115
118
|
return selectedSnapshot;
|
|
116
119
|
}
|
|
@@ -139,19 +142,22 @@ function shallowEqual(objA, objB) {
|
|
|
139
142
|
return true;
|
|
140
143
|
}
|
|
141
144
|
|
|
142
|
-
function createActorContext(actorLogic,
|
|
145
|
+
function createActorContext(actorLogic, actorOptions) {
|
|
143
146
|
const ReactContext = /*#__PURE__*/React.createContext(null);
|
|
144
147
|
const OriginalProvider = ReactContext.Provider;
|
|
145
148
|
function Provider({
|
|
146
149
|
children,
|
|
147
150
|
logic: providedLogic = actorLogic,
|
|
148
151
|
machine,
|
|
149
|
-
options: providedOptions
|
|
152
|
+
options: providedOptions
|
|
150
153
|
}) {
|
|
151
154
|
if (machine) {
|
|
152
155
|
throw new Error(`The "machine" prop has been deprecated. Please use "logic" instead.`);
|
|
153
156
|
}
|
|
154
|
-
const actor = useActorRef(providedLogic,
|
|
157
|
+
const actor = useActorRef(providedLogic, {
|
|
158
|
+
...actorOptions,
|
|
159
|
+
...providedOptions
|
|
160
|
+
});
|
|
155
161
|
return /*#__PURE__*/React.createElement(OriginalProvider, {
|
|
156
162
|
value: actor,
|
|
157
163
|
children
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@xstate/react",
|
|
3
|
-
"version": "4.0
|
|
3
|
+
"version": "4.1.0",
|
|
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.
|
|
58
|
+
"xstate": "^5.6.2"
|
|
59
59
|
},
|
|
60
60
|
"peerDependenciesMeta": {
|
|
61
61
|
"xstate": {
|
|
@@ -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.
|
|
79
|
+
"xstate": "5.6.2"
|
|
80
80
|
}
|
|
81
81
|
}
|