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

Commit cc52991

Browse files
committed
Add persisting state to README
1 parent b6d1582 commit cc52991

File tree

1 file changed

+192
-3
lines changed

1 file changed

+192
-3
lines changed

README.md

Lines changed: 192 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ npm i react-xstate-js -S
1212

1313
# Using
1414
## Example 1 - reading & changing state
15-
```js
15+
```jsx
1616
import React from 'react';
1717
import { Machine } from 'react-xstate-js';
1818

@@ -135,7 +135,7 @@ const MyComponent: React.SFC = () => (
135135
```
136136

137137
## Example 2 - options (with actions)
138-
```js
138+
```jsx
139139
import React from 'react';
140140
import { Machine } from 'react-xstate-js';
141141

@@ -446,6 +446,149 @@ const MyComponent: React.SFC = () => (
446446
)
447447
```
448448

449+
## Example 4 - persisting state
450+
```jsx
451+
import React from 'react';
452+
import {
453+
Machine,
454+
} from 'react-xstate-js'
455+
456+
const myMachineConfig = {
457+
key: 'example1',
458+
initial: 'step1',
459+
states: {
460+
step1: {
461+
on: {
462+
NEXT: 'step2',
463+
},
464+
},
465+
step2: {
466+
on: {
467+
PREVIOUS: 'step1',
468+
NEXT: 'step3',
469+
},
470+
},
471+
step3: {
472+
on: {
473+
PREVIOUS: 'step2',
474+
},
475+
},
476+
},
477+
}
478+
479+
const mySavedJSONState = `{ "actions": [], "activities": {}, "meta": {}, "events": [], "value": "step3", "event": { "type": "NEXT" }, "historyValue": { "current": "step3", "states": {} }, "history": { "actions": [], "activities": {}, "meta": {}, "events": [], "value": "step2", "event": { "type": "NEXT" }, "historyValue": { "current": "step2", "states": {} }, "history": { "actions": [], "activities": {}, "meta": {}, "events": [], "value": "step1", "event": { "type": "xstate.init" } } } }`
480+
481+
const mySavedState = JSON.parse(mySavedJSONState)
482+
483+
const MyComponent = () => (
484+
<Machine
485+
config={myMachineConfig}
486+
savedState={mySavedState}
487+
>
488+
{({ service, state }) => (
489+
<>
490+
<button
491+
type="button"
492+
onClick={() => service.send({ type: 'PREVIOUS' })}
493+
>
494+
previous
495+
</button>
496+
<button
497+
type="button"
498+
onClick={() => service.send({ type: 'NEXT' })}
499+
>
500+
next
501+
</button>
502+
<p>
503+
state:
504+
{' '}
505+
{JSON.stringify(state.value)}
506+
</p>
507+
</>
508+
)}
509+
</Machine>
510+
)
511+
512+
export default MyComponent
513+
```
514+
515+
## Example 4 - TypeScript
516+
```tsx
517+
import React from 'react';
518+
import {
519+
MachineConfig, Machine, State, Interpreter
520+
} from 'react-xstate-js'
521+
522+
interface MyStateSchema {
523+
states: {
524+
step1: {}
525+
step2: {}
526+
step3: {}
527+
}
528+
}
529+
530+
type MyEvent = { type: 'NEXT' }
531+
| { type: 'PREVIOUS' }
532+
533+
const myMachineConfig: MachineConfig<{}, MyStateSchema, MyEvent> = {
534+
key: 'example1',
535+
initial: 'step1',
536+
states: {
537+
step1: {
538+
on: {
539+
NEXT: 'step2',
540+
},
541+
},
542+
step2: {
543+
on: {
544+
PREVIOUS: 'step1',
545+
NEXT: 'step3',
546+
},
547+
},
548+
step3: {
549+
on: {
550+
PREVIOUS: 'step2',
551+
},
552+
},
553+
},
554+
}
555+
556+
const mySavedJSONState = `{ "actions": [], "activities": {}, "meta": {}, "events": [], "value": "step3", "event": { "type": "NEXT" }, "historyValue": { "current": "step3", "states": {} }, "history": { "actions": [], "activities": {}, "meta": {}, "events": [], "value": "step2", "event": { "type": "NEXT" }, "historyValue": { "current": "step2", "states": {} }, "history": { "actions": [], "activities": {}, "meta": {}, "events": [], "value": "step1", "event": { "type": "xstate.init" } } } }`
557+
558+
const mySavedState: State<{}, MyEvent> = JSON.parse(mySavedJSONState)
559+
560+
const MyComponent: React.SFC = () => (
561+
<Machine
562+
config={myMachineConfig}
563+
savedState={mySavedState}
564+
>
565+
{({ service, state }: { service: Interpreter<{}, MyStateSchema, MyEvent>, state: State<{}, MyEvent> }) => (
566+
<>
567+
<button
568+
type="button"
569+
onClick={() => service.send({ type: 'PREVIOUS' })}
570+
>
571+
previous
572+
</button>
573+
<button
574+
type="button"
575+
onClick={() => service.send({ type: 'NEXT' })}
576+
>
577+
next
578+
</button>
579+
<p>
580+
state:
581+
{' '}
582+
{JSON.stringify(state.value)}
583+
</p>
584+
</>
585+
)}
586+
</Machine>
587+
)
588+
589+
export default MyComponent
590+
```
591+
449592
# API
450593
## \<Machine \/\>
451594
A [React](https://reactjs.org/) interpreter for [xstate](https://github.com/davidkpiano/xstate).
@@ -498,6 +641,52 @@ const myMachineOptions = {
498641
};
499642
```
500643

644+
#### savedState: xstate [State](https://xstate.js.org/api/classes/state.html).
645+
```js
646+
const savedState = {
647+
"actions": [],
648+
"activities": {},
649+
"meta": {},
650+
"events": [],
651+
"value": "step3",
652+
"event": {
653+
"type": "NEXT"
654+
},
655+
"historyValue": {
656+
"current": "step3",
657+
"states": {}
658+
},
659+
"history": {
660+
"actions": [],
661+
"activities": {},
662+
"meta": {},
663+
"events": [],
664+
"value": "step2",
665+
"event": {
666+
"type": "NEXT"
667+
},
668+
"historyValue": {
669+
"current": "step2",
670+
"states": {}
671+
},
672+
"history": {
673+
"actions": [],
674+
"activities": {},
675+
"meta": {},
676+
"events": [],
677+
"value": "step1",
678+
"event": {
679+
"type": "PREVIOUS"
680+
},
681+
"historyValue": {
682+
"current": "step1",
683+
"states": {}
684+
}
685+
}
686+
}
687+
};
688+
```
689+
501690
### Return
502691
#### service: xstate [interpreter](https://xstate.js.org/docs/guides/interpretation.html).
503692
```jsx
@@ -515,4 +704,4 @@ const myMachineOptions = {
515704
...
516705
)}
517706
</Machine>
518-
```
707+
```

0 commit comments

Comments
 (0)