@xstate/react
Advanced tools
Comparing version 1.0.0-rc.0 to 1.0.0-rc.1
import { StateMachine, EventObject } from '@xstate/fsm'; | ||
export declare function useMachine<TC, TE extends EventObject>(stateMachine: StateMachine.Machine<TC, TE, any>): [StateMachine.State<TC, TE, any>, StateMachine.Service<TC, TE>['send'], StateMachine.Service<TC, TE>]; | ||
import { AnyEventObject } from 'xstate'; | ||
export declare function useMachine<TC, TE extends EventObject = AnyEventObject>(stateMachine: StateMachine.Machine<TC, TE, any>): [StateMachine.State<TC, TE, any>, StateMachine.Service<TC, TE>['send'], StateMachine.Service<TC, TE>]; | ||
//# sourceMappingURL=fsm.d.ts.map |
@@ -86,6 +86,5 @@ "use strict"; | ||
// Keep track of the current machine state | ||
var initialState = rehydratedState | ||
? xstate_1.State.create(rehydratedState) | ||
: service.initialState; | ||
var _a = __read(react_1.useState(function () { return initialState; }), 2), current = _a[0], setCurrent = _a[1]; | ||
var _a = __read(react_1.useState(function () { | ||
return rehydratedState ? xstate_1.State.create(rehydratedState) : service.initialState; | ||
}), 2), current = _a[0], setCurrent = _a[1]; | ||
// Start service immediately (before mount) if specified in options | ||
@@ -95,3 +94,3 @@ if (immediate) { | ||
} | ||
react_1.useEffect(function () { | ||
react_1.useLayoutEffect(function () { | ||
// Start the service when the component mounts. | ||
@@ -101,3 +100,3 @@ // Note: the service will start only if it hasn't started already. | ||
// If a rehydrated state was provided, use the resolved `initialState`. | ||
service.start(rehydratedState ? initialState : undefined); | ||
service.start(rehydratedState ? current : undefined); | ||
return function () { | ||
@@ -112,10 +111,10 @@ // Stop the service when the component unmounts | ||
function useService(service) { | ||
var _a = __read(react_1.useState(service.state), 2), current = _a[0], setCurrent = _a[1]; | ||
react_1.useEffect(function () { | ||
var _a = __read(react_1.useState(service.state || service.initialState), 2), current = _a[0], setCurrent = _a[1]; | ||
react_1.useLayoutEffect(function () { | ||
// Set to current service state as there is a possibility | ||
// of a transition occurring between the initial useState() | ||
// initialization and useEffect() commit. | ||
setCurrent(service.state); | ||
setCurrent(service.state || service.initialState); | ||
var listener = function (state) { | ||
if (state.changed) { | ||
if (state.changed !== false) { | ||
setCurrent(state); | ||
@@ -122,0 +121,0 @@ } |
@@ -26,3 +26,3 @@ "use strict"; | ||
actorRef.current = actor; | ||
var sub_1 = actor.subscribe(setCurrent); | ||
var sub_1 = actor.subscribe(function (current) { return setCurrent(current); }); | ||
return function () { | ||
@@ -29,0 +29,0 @@ sub_1.unsubscribe(); |
{ | ||
"name": "@xstate/react", | ||
"version": "1.0.0-rc.0", | ||
"version": "1.0.0-rc.1", | ||
"description": "XState tools for React", | ||
@@ -34,3 +34,2 @@ "keywords": [ | ||
"clean": "rm -rf dist lib tsconfig.tsbuildinfo", | ||
"prebuild": "npm run clean", | ||
"build": "tsc", | ||
@@ -49,7 +48,6 @@ "test": "jest", | ||
"@testing-library/react": "^8.0.9", | ||
"@types/jest": "^24.0.11", | ||
"@types/jsdom": "^12.2.3", | ||
"@types/react": "^16.9.0", | ||
"@types/react-dom": "^16.8.5", | ||
"@xstate/fsm": "^1.0.3", | ||
"@types/react": "^16.9.11", | ||
"@types/react-dom": "^16.9.4", | ||
"@xstate/fsm": "*", | ||
"jest": "^24.8.0", | ||
@@ -59,8 +57,8 @@ "jsdom": "^14.0.0", | ||
"lerna-alias": "3.0.3-0", | ||
"react": "^16.9.0", | ||
"react-dom": "^16.9.0", | ||
"ts-jest": "^24.0.2", | ||
"typescript": "^3.6.2", | ||
"xstate": "^4.7.0-rc6" | ||
"react": "^16.12.0", | ||
"react-dom": "^16.12.0", | ||
"ts-jest": "^24.1.9", | ||
"typescript": "^3.7.2", | ||
"xstate": "*" | ||
} | ||
} |
19864
14