Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@xstate/react

Package Overview
Dependencies
Maintainers
3
Versions
79
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@xstate/react - npm Package Compare versions

Comparing version 4.1.1 to 4.1.2

4

dist/declarations/src/createActorContext.d.ts

@@ -9,5 +9,3 @@ import * as React from 'react';

options?: ActorOptions<TLogic>;
/**
* @deprecated Use `logic` instead.
*/
/** @deprecated Use `logic` instead. */
machine?: never;

@@ -14,0 +12,0 @@ logic?: TLogic;

@@ -0,6 +1,6 @@

export { createActorContext } from "./createActorContext.js";
export { shallowEqual } from "./shallowEqual.js";
export { useActor } from "./useActor.js";
export { useActorRef } from "./useActorRef.js";
export { useSelector } from "./useSelector.js";
export { shallowEqual } from "./shallowEqual.js";
export { createActorContext } from "./createActorContext.js";
export { useMachine } from "./useMachine.js";

@@ -1,2 +0,6 @@

import { Actor, ActorOptions, AnyActorLogic, SnapshotFrom } from 'xstate';
export declare function useActor<TLogic extends AnyActorLogic>(logic: TLogic, options?: ActorOptions<TLogic>): [SnapshotFrom<TLogic>, Actor<TLogic>['send'], Actor<TLogic>];
import { Actor, ActorOptions, AnyActorLogic, SnapshotFrom, type ConditionalRequired, type IsNotNever, type RequiredActorOptionsKeys } from 'xstate';
export declare function useActor<TLogic extends AnyActorLogic>(logic: TLogic, ...[options]: ConditionalRequired<[
options?: ActorOptions<TLogic> & {
[K in RequiredActorOptionsKeys<TLogic>]: unknown;
}
], IsNotNever<RequiredActorOptionsKeys<TLogic>>>): [SnapshotFrom<TLogic>, Actor<TLogic>['send'], Actor<TLogic>];

@@ -1,3 +0,15 @@

import { Actor, ActorOptions, AnyActorLogic, Observer, SnapshotFrom } from 'xstate';
export declare function useIdleActorRef<TLogic extends AnyActorLogic>(logic: TLogic, options: Partial<ActorOptions<TLogic>>): Actor<TLogic>;
export declare function useActorRef<TLogic extends AnyActorLogic>(machine: TLogic, options?: ActorOptions<TLogic>, observerOrListener?: Observer<SnapshotFrom<TLogic>> | ((value: SnapshotFrom<TLogic>) => void)): Actor<TLogic>;
import { Actor, ActorOptions, AnyActorLogic, Observer, SnapshotFrom, type ConditionalRequired, type IsNotNever, type RequiredActorOptionsKeys } from 'xstate';
export declare function useIdleActorRef<TLogic extends AnyActorLogic>(logic: TLogic, ...[options]: ConditionalRequired<[
options?: ActorOptions<TLogic> & {
[K in RequiredActorOptionsKeys<TLogic>]: unknown;
}
], IsNotNever<RequiredActorOptionsKeys<TLogic>>>): Actor<TLogic>;
export declare function useActorRef<TLogic extends AnyActorLogic>(machine: TLogic, ...[options, observerOrListener]: IsNotNever<RequiredActorOptionsKeys<TLogic>> extends true ? [
options: ActorOptions<TLogic> & {
[K in RequiredActorOptionsKeys<TLogic>]: unknown;
},
observerOrListener?: Observer<SnapshotFrom<TLogic>> | ((value: SnapshotFrom<TLogic>) => void)
] : [
options?: ActorOptions<TLogic>,
observerOrListener?: Observer<SnapshotFrom<TLogic>> | ((value: SnapshotFrom<TLogic>) => void)
]): Actor<TLogic>;

@@ -1,5 +0,7 @@

import { Actor, ActorOptions, AnyStateMachine, StateFrom } from 'xstate';
/**
* @alias useActor
*/
export declare function useMachine<TMachine extends AnyStateMachine>(machine: TMachine, options?: ActorOptions<TMachine>): [StateFrom<TMachine>, Actor<TMachine>['send'], Actor<TMachine>];
import { Actor, ActorOptions, AnyStateMachine, StateFrom, type ConditionalRequired, type IsNotNever, type RequiredActorOptionsKeys } from 'xstate';
/** @alias useActor */
export declare function useMachine<TMachine extends AnyStateMachine>(machine: TMachine, ...[options]: ConditionalRequired<[
options?: ActorOptions<TMachine> & {
[K in RequiredActorOptionsKeys<TMachine>]: unknown;
}
], IsNotNever<RequiredActorOptionsKeys<TMachine>>>): [StateFrom<TMachine>, Actor<TMachine>['send'], Actor<TMachine>];

@@ -6,6 +6,6 @@ 'use strict';

var React = require('react');
var shim = require('use-sync-external-store/shim');
var useIsomorphicLayoutEffect = require('use-isomorphic-layout-effect');
var xstate = require('xstate');
var withSelector = require('use-sync-external-store/shim/with-selector');
var shim = require('use-sync-external-store/shim');

@@ -67,3 +67,3 @@ function _interopDefault (e) { return e && e.__esModule ? e : { 'default': e }; }

function useIdleActorRef(logic, options) {
function useIdleActorRef(logic, ...[options]) {
let [[currentConfig, actorRef], setCurrent] = React.useState(() => {

@@ -90,3 +90,3 @@ const actorRef = xstate.createActor(logic, options);

}
function useActorRef(machine, options = {}, observerOrListener) {
function useActorRef(machine, ...[options, observerOrListener]) {
const actorRef = useIdleActorRef(machine, options);

@@ -111,23 +111,2 @@ React.useEffect(() => {

function useActor(logic, options = {}) {
const actorRef = useIdleActorRef(logic, options);
const getSnapshot = React.useCallback(() => {
return actorRef.getSnapshot();
}, [actorRef]);
const subscribe = React.useCallback(handleStoreChange => {
const {
unsubscribe
} = actorRef.subscribe(handleStoreChange);
return unsubscribe;
}, [actorRef]);
const actorSnapshot = shim.useSyncExternalStore(subscribe, getSnapshot, getSnapshot);
React.useEffect(() => {
actorRef.start();
return () => {
stopRootWithRehydration(actorRef);
};
}, [actorRef]);
return [actorSnapshot, actorRef.send, actorRef];
}
function defaultCompare(a, b) {

@@ -151,26 +130,2 @@ return a === b;

// From https://github.com/reduxjs/react-redux/blob/720f0ba79236cdc3e1115f4ef9a7760a21784b48/src/utils/shallowEqual.ts
function is(x, y) {
if (x === y) {
return x !== 0 || y !== 0 || 1 / x === 1 / y;
} else {
return x !== x && y !== y;
}
}
function shallowEqual(objA, objB) {
if (is(objA, objB)) return true;
if (typeof objA !== 'object' || objA === null || typeof objB !== 'object' || objB === null) {
return false;
}
const keysA = Object.keys(objA);
const keysB = Object.keys(objB);
if (keysA.length !== keysB.length) return false;
for (let i = 0; i < keysA.length; i++) {
if (!Object.prototype.hasOwnProperty.call(objB, keysA[i]) || !is(objA[keysA[i]], objB[keysA[i]])) {
return false;
}
}
return true;
}
function createActorContext(actorLogic, actorOptions) {

@@ -218,6 +173,49 @@ const ReactContext = /*#__PURE__*/React__namespace.createContext(null);

/**
* @alias useActor
*/
function useMachine(machine, options = {}) {
// From https://github.com/reduxjs/react-redux/blob/720f0ba79236cdc3e1115f4ef9a7760a21784b48/src/utils/shallowEqual.ts
function is(x, y) {
if (x === y) {
return x !== 0 || y !== 0 || 1 / x === 1 / y;
} else {
return x !== x && y !== y;
}
}
function shallowEqual(objA, objB) {
if (is(objA, objB)) return true;
if (typeof objA !== 'object' || objA === null || typeof objB !== 'object' || objB === null) {
return false;
}
const keysA = Object.keys(objA);
const keysB = Object.keys(objB);
if (keysA.length !== keysB.length) return false;
for (let i = 0; i < keysA.length; i++) {
if (!Object.prototype.hasOwnProperty.call(objB, keysA[i]) || !is(objA[keysA[i]], objB[keysA[i]])) {
return false;
}
}
return true;
}
function useActor(logic, ...[options]) {
const actorRef = useIdleActorRef(logic, options);
const getSnapshot = React.useCallback(() => {
return actorRef.getSnapshot();
}, [actorRef]);
const subscribe = React.useCallback(handleStoreChange => {
const {
unsubscribe
} = actorRef.subscribe(handleStoreChange);
return unsubscribe;
}, [actorRef]);
const actorSnapshot = shim.useSyncExternalStore(subscribe, getSnapshot, getSnapshot);
React.useEffect(() => {
actorRef.start();
return () => {
stopRootWithRehydration(actorRef);
};
}, [actorRef]);
return [actorSnapshot, actorRef.send, actorRef];
}
/** @alias useActor */
function useMachine(machine, ...[options]) {
return useActor(machine, options);

@@ -224,0 +222,0 @@ }

@@ -6,6 +6,6 @@ 'use strict';

var React = require('react');
var shim = require('use-sync-external-store/shim');
var useIsomorphicLayoutEffect = require('use-isomorphic-layout-effect');
var xstate = require('xstate');
var withSelector = require('use-sync-external-store/shim/with-selector');
var shim = require('use-sync-external-store/shim');

@@ -67,3 +67,3 @@ function _interopDefault (e) { return e && e.__esModule ? e : { 'default': e }; }

function useIdleActorRef(logic, options) {
function useIdleActorRef(logic, ...[options]) {
let [[currentConfig, actorRef], setCurrent] = React.useState(() => {

@@ -90,3 +90,3 @@ const actorRef = xstate.createActor(logic, options);

}
function useActorRef(machine, options = {}, observerOrListener) {
function useActorRef(machine, ...[options, observerOrListener]) {
const actorRef = useIdleActorRef(machine, options);

@@ -111,26 +111,2 @@ React.useEffect(() => {

function useActor(logic, options = {}) {
if (!!logic && 'send' in logic && typeof logic.send === 'function') {
throw new Error(`useActor() expects actor logic (e.g. a machine), but received an ActorRef. Use the useSelector(actorRef, ...) hook instead to read the ActorRef's snapshot.`);
}
const actorRef = useIdleActorRef(logic, options);
const getSnapshot = React.useCallback(() => {
return actorRef.getSnapshot();
}, [actorRef]);
const subscribe = React.useCallback(handleStoreChange => {
const {
unsubscribe
} = actorRef.subscribe(handleStoreChange);
return unsubscribe;
}, [actorRef]);
const actorSnapshot = shim.useSyncExternalStore(subscribe, getSnapshot, getSnapshot);
React.useEffect(() => {
actorRef.start();
return () => {
stopRootWithRehydration(actorRef);
};
}, [actorRef]);
return [actorSnapshot, actorRef.send, actorRef];
}
function defaultCompare(a, b) {

@@ -154,26 +130,2 @@ return a === b;

// From https://github.com/reduxjs/react-redux/blob/720f0ba79236cdc3e1115f4ef9a7760a21784b48/src/utils/shallowEqual.ts
function is(x, y) {
if (x === y) {
return x !== 0 || y !== 0 || 1 / x === 1 / y;
} else {
return x !== x && y !== y;
}
}
function shallowEqual(objA, objB) {
if (is(objA, objB)) return true;
if (typeof objA !== 'object' || objA === null || typeof objB !== 'object' || objB === null) {
return false;
}
const keysA = Object.keys(objA);
const keysB = Object.keys(objB);
if (keysA.length !== keysB.length) return false;
for (let i = 0; i < keysA.length; i++) {
if (!Object.prototype.hasOwnProperty.call(objB, keysA[i]) || !is(objA[keysA[i]], objB[keysA[i]])) {
return false;
}
}
return true;
}
function createActorContext(actorLogic, actorOptions) {

@@ -221,6 +173,52 @@ const ReactContext = /*#__PURE__*/React__namespace.createContext(null);

/**
* @alias useActor
*/
function useMachine(machine, options = {}) {
// From https://github.com/reduxjs/react-redux/blob/720f0ba79236cdc3e1115f4ef9a7760a21784b48/src/utils/shallowEqual.ts
function is(x, y) {
if (x === y) {
return x !== 0 || y !== 0 || 1 / x === 1 / y;
} else {
return x !== x && y !== y;
}
}
function shallowEqual(objA, objB) {
if (is(objA, objB)) return true;
if (typeof objA !== 'object' || objA === null || typeof objB !== 'object' || objB === null) {
return false;
}
const keysA = Object.keys(objA);
const keysB = Object.keys(objB);
if (keysA.length !== keysB.length) return false;
for (let i = 0; i < keysA.length; i++) {
if (!Object.prototype.hasOwnProperty.call(objB, keysA[i]) || !is(objA[keysA[i]], objB[keysA[i]])) {
return false;
}
}
return true;
}
function useActor(logic, ...[options]) {
if (!!logic && 'send' in logic && typeof logic.send === 'function') {
throw new Error(`useActor() expects actor logic (e.g. a machine), but received an ActorRef. Use the useSelector(actorRef, ...) hook instead to read the ActorRef's snapshot.`);
}
const actorRef = useIdleActorRef(logic, options);
const getSnapshot = React.useCallback(() => {
return actorRef.getSnapshot();
}, [actorRef]);
const subscribe = React.useCallback(handleStoreChange => {
const {
unsubscribe
} = actorRef.subscribe(handleStoreChange);
return unsubscribe;
}, [actorRef]);
const actorSnapshot = shim.useSyncExternalStore(subscribe, getSnapshot, getSnapshot);
React.useEffect(() => {
actorRef.start();
return () => {
stopRootWithRehydration(actorRef);
};
}, [actorRef]);
return [actorSnapshot, actorRef.send, actorRef];
}
/** @alias useActor */
function useMachine(machine, ...[options]) {
return useActor(machine, options);

@@ -227,0 +225,0 @@ }

import * as React from 'react';
import { useEffect, useState, useCallback } from 'react';
import { useSyncExternalStore } from 'use-sync-external-store/shim';
import useIsomorphicLayoutEffect from 'use-isomorphic-layout-effect';
import { toObserver, createActor } from 'xstate';
import { useSyncExternalStoreWithSelector } from 'use-sync-external-store/shim/with-selector';
import { useSyncExternalStore } from 'use-sync-external-store/shim';

@@ -40,3 +40,3 @@ const forEachActor = (actorRef, callback) => {

function useIdleActorRef(logic, options) {
function useIdleActorRef(logic, ...[options]) {
let [[currentConfig, actorRef], setCurrent] = useState(() => {

@@ -63,3 +63,3 @@ const actorRef = createActor(logic, options);

}
function useActorRef(machine, options = {}, observerOrListener) {
function useActorRef(machine, ...[options, observerOrListener]) {
const actorRef = useIdleActorRef(machine, options);

@@ -84,26 +84,2 @@ useEffect(() => {

function useActor(logic, options = {}) {
if (!!logic && 'send' in logic && typeof logic.send === 'function') {
throw new Error(`useActor() expects actor logic (e.g. a machine), but received an ActorRef. Use the useSelector(actorRef, ...) hook instead to read the ActorRef's snapshot.`);
}
const actorRef = useIdleActorRef(logic, options);
const getSnapshot = useCallback(() => {
return actorRef.getSnapshot();
}, [actorRef]);
const subscribe = useCallback(handleStoreChange => {
const {
unsubscribe
} = actorRef.subscribe(handleStoreChange);
return unsubscribe;
}, [actorRef]);
const actorSnapshot = useSyncExternalStore(subscribe, getSnapshot, getSnapshot);
useEffect(() => {
actorRef.start();
return () => {
stopRootWithRehydration(actorRef);
};
}, [actorRef]);
return [actorSnapshot, actorRef.send, actorRef];
}
function defaultCompare(a, b) {

@@ -127,26 +103,2 @@ return a === b;

// From https://github.com/reduxjs/react-redux/blob/720f0ba79236cdc3e1115f4ef9a7760a21784b48/src/utils/shallowEqual.ts
function is(x, y) {
if (x === y) {
return x !== 0 || y !== 0 || 1 / x === 1 / y;
} else {
return x !== x && y !== y;
}
}
function shallowEqual(objA, objB) {
if (is(objA, objB)) return true;
if (typeof objA !== 'object' || objA === null || typeof objB !== 'object' || objB === null) {
return false;
}
const keysA = Object.keys(objA);
const keysB = Object.keys(objB);
if (keysA.length !== keysB.length) return false;
for (let i = 0; i < keysA.length; i++) {
if (!Object.prototype.hasOwnProperty.call(objB, keysA[i]) || !is(objA[keysA[i]], objB[keysA[i]])) {
return false;
}
}
return true;
}
function createActorContext(actorLogic, actorOptions) {

@@ -194,6 +146,52 @@ const ReactContext = /*#__PURE__*/React.createContext(null);

/**
* @alias useActor
*/
function useMachine(machine, options = {}) {
// From https://github.com/reduxjs/react-redux/blob/720f0ba79236cdc3e1115f4ef9a7760a21784b48/src/utils/shallowEqual.ts
function is(x, y) {
if (x === y) {
return x !== 0 || y !== 0 || 1 / x === 1 / y;
} else {
return x !== x && y !== y;
}
}
function shallowEqual(objA, objB) {
if (is(objA, objB)) return true;
if (typeof objA !== 'object' || objA === null || typeof objB !== 'object' || objB === null) {
return false;
}
const keysA = Object.keys(objA);
const keysB = Object.keys(objB);
if (keysA.length !== keysB.length) return false;
for (let i = 0; i < keysA.length; i++) {
if (!Object.prototype.hasOwnProperty.call(objB, keysA[i]) || !is(objA[keysA[i]], objB[keysA[i]])) {
return false;
}
}
return true;
}
function useActor(logic, ...[options]) {
if (!!logic && 'send' in logic && typeof logic.send === 'function') {
throw new Error(`useActor() expects actor logic (e.g. a machine), but received an ActorRef. Use the useSelector(actorRef, ...) hook instead to read the ActorRef's snapshot.`);
}
const actorRef = useIdleActorRef(logic, options);
const getSnapshot = useCallback(() => {
return actorRef.getSnapshot();
}, [actorRef]);
const subscribe = useCallback(handleStoreChange => {
const {
unsubscribe
} = actorRef.subscribe(handleStoreChange);
return unsubscribe;
}, [actorRef]);
const actorSnapshot = useSyncExternalStore(subscribe, getSnapshot, getSnapshot);
useEffect(() => {
actorRef.start();
return () => {
stopRootWithRehydration(actorRef);
};
}, [actorRef]);
return [actorSnapshot, actorRef.send, actorRef];
}
/** @alias useActor */
function useMachine(machine, ...[options]) {
return useActor(machine, options);

@@ -200,0 +198,0 @@ }

import * as React from 'react';
import { useEffect, useState, useCallback } from 'react';
import { useSyncExternalStore } from 'use-sync-external-store/shim';
import useIsomorphicLayoutEffect from 'use-isomorphic-layout-effect';
import { toObserver, createActor } from 'xstate';
import { useSyncExternalStoreWithSelector } from 'use-sync-external-store/shim/with-selector';
import { useSyncExternalStore } from 'use-sync-external-store/shim';

@@ -40,3 +40,3 @@ const forEachActor = (actorRef, callback) => {

function useIdleActorRef(logic, options) {
function useIdleActorRef(logic, ...[options]) {
let [[currentConfig, actorRef], setCurrent] = useState(() => {

@@ -63,3 +63,3 @@ const actorRef = createActor(logic, options);

}
function useActorRef(machine, options = {}, observerOrListener) {
function useActorRef(machine, ...[options, observerOrListener]) {
const actorRef = useIdleActorRef(machine, options);

@@ -84,23 +84,2 @@ useEffect(() => {

function useActor(logic, options = {}) {
const actorRef = useIdleActorRef(logic, options);
const getSnapshot = useCallback(() => {
return actorRef.getSnapshot();
}, [actorRef]);
const subscribe = useCallback(handleStoreChange => {
const {
unsubscribe
} = actorRef.subscribe(handleStoreChange);
return unsubscribe;
}, [actorRef]);
const actorSnapshot = useSyncExternalStore(subscribe, getSnapshot, getSnapshot);
useEffect(() => {
actorRef.start();
return () => {
stopRootWithRehydration(actorRef);
};
}, [actorRef]);
return [actorSnapshot, actorRef.send, actorRef];
}
function defaultCompare(a, b) {

@@ -124,26 +103,2 @@ return a === b;

// From https://github.com/reduxjs/react-redux/blob/720f0ba79236cdc3e1115f4ef9a7760a21784b48/src/utils/shallowEqual.ts
function is(x, y) {
if (x === y) {
return x !== 0 || y !== 0 || 1 / x === 1 / y;
} else {
return x !== x && y !== y;
}
}
function shallowEqual(objA, objB) {
if (is(objA, objB)) return true;
if (typeof objA !== 'object' || objA === null || typeof objB !== 'object' || objB === null) {
return false;
}
const keysA = Object.keys(objA);
const keysB = Object.keys(objB);
if (keysA.length !== keysB.length) return false;
for (let i = 0; i < keysA.length; i++) {
if (!Object.prototype.hasOwnProperty.call(objB, keysA[i]) || !is(objA[keysA[i]], objB[keysA[i]])) {
return false;
}
}
return true;
}
function createActorContext(actorLogic, actorOptions) {

@@ -191,6 +146,49 @@ const ReactContext = /*#__PURE__*/React.createContext(null);

/**
* @alias useActor
*/
function useMachine(machine, options = {}) {
// From https://github.com/reduxjs/react-redux/blob/720f0ba79236cdc3e1115f4ef9a7760a21784b48/src/utils/shallowEqual.ts
function is(x, y) {
if (x === y) {
return x !== 0 || y !== 0 || 1 / x === 1 / y;
} else {
return x !== x && y !== y;
}
}
function shallowEqual(objA, objB) {
if (is(objA, objB)) return true;
if (typeof objA !== 'object' || objA === null || typeof objB !== 'object' || objB === null) {
return false;
}
const keysA = Object.keys(objA);
const keysB = Object.keys(objB);
if (keysA.length !== keysB.length) return false;
for (let i = 0; i < keysA.length; i++) {
if (!Object.prototype.hasOwnProperty.call(objB, keysA[i]) || !is(objA[keysA[i]], objB[keysA[i]])) {
return false;
}
}
return true;
}
function useActor(logic, ...[options]) {
const actorRef = useIdleActorRef(logic, options);
const getSnapshot = useCallback(() => {
return actorRef.getSnapshot();
}, [actorRef]);
const subscribe = useCallback(handleStoreChange => {
const {
unsubscribe
} = actorRef.subscribe(handleStoreChange);
return unsubscribe;
}, [actorRef]);
const actorSnapshot = useSyncExternalStore(subscribe, getSnapshot, getSnapshot);
useEffect(() => {
actorRef.start();
return () => {
stopRootWithRehydration(actorRef);
};
}, [actorRef]);
return [actorSnapshot, actorRef.send, actorRef];
}
/** @alias useActor */
function useMachine(machine, ...[options]) {
return useActor(machine, options);

@@ -197,0 +195,0 @@ }

{
"name": "@xstate/react",
"version": "4.1.1",
"version": "4.1.2",
"description": "XState tools for React",

@@ -58,3 +58,3 @@ "keywords": [

"react": "^16.8.0 || ^17.0.0 || ^18.0.0",
"xstate": "^5.11.0"
"xstate": "^5.18.1"
},

@@ -71,13 +71,11 @@ "peerDependenciesMeta": {

"devDependencies": {
"@testing-library/react": "^14.2.1",
"@types/jsdom": "^12.2.3",
"@types/react": "^17.0.43",
"@types/react-dom": "^17.0.14",
"@testing-library/react": "^16.0.0",
"@types/react": "^18.3.3",
"@types/react-dom": "^18.3.0",
"@types/use-sync-external-store": "^0.0.3",
"jsdom": "^14.0.0",
"jsdom-global": "^3.0.2",
"react": "^18.0.0",
"react-dom": "^18.0.0",
"xstate": "5.11.0"
"rxjs": "^7.8.0",
"xstate": "5.18.1"
}
}
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc