From b6d158290d07c7d37d1196a1171ba93ea83e2b98 Mon Sep 17 00:00:00 2001 From: Brad Woods Date: Wed, 2 Jan 2019 11:45:05 +1100 Subject: [PATCH] Add persisting state --- src/Machine/index.tsx | 10 ++++++++-- src/types.ts | 1 + 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/src/Machine/index.tsx b/src/Machine/index.tsx index d4bdf73..f723c00 100644 --- a/src/Machine/index.tsx +++ b/src/Machine/index.tsx @@ -1,6 +1,6 @@ import * as React from 'react'; import { - Machine as XstateMachine, + Machine as XstateMachine, State } from 'xstate'; import { interpret @@ -20,12 +20,18 @@ class Machine extends React.Component { } componentDidMount() { + const { savedState } = this.props + this.service = interpret(this.machine) .onTransition(nextState => { this.setState({ machineStateNode: nextState }); }); - this.service.start(); + if (savedState) { + const restoredState = State.create(savedState) + this.service.start(restoredState) + } + else this.service.start(); } componentWillUnmount() { diff --git a/src/types.ts b/src/types.ts index a3d3305..dda6c26 100644 --- a/src/types.ts +++ b/src/types.ts @@ -18,6 +18,7 @@ export interface IProps< > { config: MachineConfig options?: MachineOptions + savedState?: State children: (args: IReturnProps) => JSX.Element | null }