Skip to content

Commit

Permalink
Deprecate interpreter.state
Browse files Browse the repository at this point in the history
  • Loading branch information
davidkpiano committed Sep 5, 2022
1 parent 2b44a4d commit 6fdaae7
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/gold-kiwis-mate.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'xstate': patch
---

Reading state directly from ~~`someService.state`~~ is deprecated. Use `someService.getSnapshot()` instead.
3 changes: 3 additions & 0 deletions packages/core/src/interpreter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,9 @@ export class Interpreter<
return this._initialState;
});
}
/**
* @deprecated Use `.getSnapshot()` instead.
*/
public get state(): State<
TContext,
TEvent,
Expand Down
2 changes: 1 addition & 1 deletion packages/xstate-react/src/useMachine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ export function useMachine<TMachine extends AnyStateMachine>(
: service.machine.initialState) as State<any, any, any, any, any>;
}

return service.state;
return service.getSnapshot();
}, [service]);

const isEqual = useCallback(
Expand Down
2 changes: 1 addition & 1 deletion packages/xstate-react/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export function partition<T, A extends T, B extends T>(
export function getServiceSnapshot<
TService extends Interpreter<any, any, any, any>
>(service: TService): TService['state'] {
return service.status !== 0 ? service.state : service.machine.initialState;
return service.getSnapshot();
}

// From https://github.com/reduxjs/react-redux/blob/master/src/utils/shallowEqual.ts
Expand Down

0 comments on commit 6fdaae7

Please sign in to comment.