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

Commit b6d1582

Browse files
committed
Add persisting state
1 parent 154d572 commit b6d1582

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

src/Machine/index.tsx

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as React from 'react';
22
import {
3-
Machine as XstateMachine,
3+
Machine as XstateMachine, State
44
} from 'xstate';
55
import {
66
interpret
@@ -20,12 +20,18 @@ class Machine extends React.Component<IProps, IState> {
2020
}
2121

2222
componentDidMount() {
23+
const { savedState } = this.props
24+
2325
this.service = interpret(this.machine)
2426
.onTransition(nextState => {
2527
this.setState({ machineStateNode: nextState });
2628
});
2729

28-
this.service.start();
30+
if (savedState) {
31+
const restoredState = State.create(savedState)
32+
this.service.start(restoredState)
33+
}
34+
else this.service.start();
2935
}
3036

3137
componentWillUnmount() {

src/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ export interface IProps<
1818
> {
1919
config: MachineConfig<TContext, TStateSchema, TEvent>
2020
options?: MachineOptions<TContext, TEvent>
21+
savedState?: State<TContext, TEvent>
2122
children: (args: IReturnProps) => JSX.Element | null
2223
}
2324

0 commit comments

Comments
 (0)