Skip to content
This repository has been archived by the owner on Apr 17, 2020. It is now read-only.

Commit

Permalink
Add persisting state
Browse files Browse the repository at this point in the history
  • Loading branch information
bradwoods committed Jan 2, 2019
1 parent 154d572 commit b6d1582
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/Machine/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as React from 'react';
import {
Machine as XstateMachine,
Machine as XstateMachine, State
} from 'xstate';
import {
interpret
Expand All @@ -20,12 +20,18 @@ class Machine extends React.Component<IProps, IState> {
}

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() {
Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export interface IProps<
> {
config: MachineConfig<TContext, TStateSchema, TEvent>
options?: MachineOptions<TContext, TEvent>
savedState?: State<TContext, TEvent>
children: (args: IReturnProps) => JSX.Element | null
}

Expand Down

0 comments on commit b6d1582

Please sign in to comment.