diff --git a/README.md b/README.md index 7a6d137..f26b612 100644 --- a/README.md +++ b/README.md @@ -11,16 +11,13 @@ npm i react-xstate-js -S ``` # Using -A playground with the following examples can be found [here](https://github.com/bradwoods/react-xstate-js-playground). - ## Example 1 - reading & changing state ```js import React from 'react'; import { Machine } from 'react-xstate-js'; -const machineConfig = { +const myMachineConfig = { key: 'example1', - strict: true, initial: 'step1', states: { step1: { @@ -43,7 +40,7 @@ const machineConfig = { }; const MyComponent = () => ( - + {({ service, state }) => ( <> + +

+ state: + {' '} + {JSON.stringify(state.value)} +

+ + )} +
+) +``` + ## Example 2 - options (with actions) ```js import React from 'react'; import { Machine } from 'react-xstate-js'; -const machineConfig = { +const myMachineConfig = { key: 'example2', - strict: true, initial: 'step1', states: { step1: { @@ -99,7 +163,7 @@ const machineConfig = { }, }; -const machineOptions = { +const myMachineOptions = { actions: { myAction: () => { console.log('myAction fired'); @@ -109,8 +173,8 @@ const machineOptions = { const MyComponent = () => ( {({ service, state }) => ( <> @@ -137,17 +201,96 @@ const MyComponent = () => ( ); ``` +## Example 2 - TypeScript +```tsx +import React from 'react'; +import { + MachineConfig, MachineOptions, Machine, State, Interpreter +} from 'react-xstate-js' + +interface MyStateSchema { + states: { + step1: {} + step2: {} + step3: {} + } +} + +type MyEvent = { type: 'NEXT' } + | { type: 'PREVIOUS' } + +const myMachineConfig: MachineConfig<{}, MyStateSchema, MyEvent> = { + key: 'example1', + initial: 'step1', + states: { + step1: { + on: { + NEXT: 'step2', + }, + }, + step2: { + onEntry: ['myAction'], + on: { + PREVIOUS: 'step1', + NEXT: 'step3', + }, + }, + step3: { + on: { + PREVIOUS: 'step2', + }, + }, + }, +} + +const myMachineOptions: MachineOptions<{}, MyEvent> = { + actions: { + myAction: () => { + console.log('myAction fired'); + }, + }, +}; + +const MyComponent: React.SFC = () => ( + + {({ service, state }: { service: Interpreter<{}, MyStateSchema, MyEvent>, state: State<{}, MyEvent> }) => ( + <> + + +

+ state: + {' '} + {JSON.stringify(state.value)} +

+ + )} +
+) +``` + ## Example 3 - context -```js +```jsx import React from 'react'; import { Machine } from 'react-xstate-js'; import { actions } from 'xstate'; const { assign } = actions; -const machineConfig = { +const myMachineConfig = { key: 'example3', - strict: true, context: { foo: '', }, @@ -173,16 +316,16 @@ const machineConfig = { }, }; -const machineOptions = { +const myMachineOptions = { actions: { - myAction: assign({ foo: ctx => 'bar' }), + myAction: assign({ foo: () => 'bar' }), }, }; const MyComponent = () => ( {({ service, state }) => ( <> @@ -214,10 +357,100 @@ const MyComponent = () => ( ); ``` +## Example 3 - TypeScript +```tsx +import React from 'react'; +import { + MachineConfig, MachineOptions, Machine, State, Interpreter, assign +} from 'react-xstate-js' + +interface MyContext { + foo: string +} + +interface MyStateSchema { + states: { + step1: {} + step2: {} + step3: {} + } +} + +type MyEvent = { type: 'NEXT' } + | { type: 'PREVIOUS' } + +const myMachineConfig: MachineConfig = { + key: 'example1', + context: { + foo: '', + }, + initial: 'step1', + states: { + step1: { + on: { + NEXT: 'step2', + }, + }, + step2: { + onEntry: ['myAction'], + on: { + PREVIOUS: 'step1', + NEXT: 'step3', + }, + }, + step3: { + on: { + PREVIOUS: 'step2', + }, + }, + }, +} + +const myMachineOptions: MachineOptions = { + actions: { + myAction: assign({ foo: () => 'bar' }), + }, +}; + +const MyComponent: React.SFC = () => ( + + {({ service, state }: { service: Interpreter, state: State }) => ( + <> + + +

+ state: + {' '} + {JSON.stringify(state.value)} +

+

+ context: + {' '} + {JSON.stringify(state.context)} +

+ + )} +
+) +``` + # API ## \ A [React](https://reactjs.org/) interpreter for [xstate](https://github.com/davidkpiano/xstate). -```js +```jsx { console.log('myAction fired'); @@ -269,7 +501,7 @@ const machineOptions = { ### Return #### service: xstate [interpreter](https://xstate.js.org/docs/guides/interpretation.html). -```js +```jsx {({ service }) => ( ... @@ -278,7 +510,7 @@ const machineOptions = { ``` #### state: xstate [state](https://xstate.js.org/api/classes/state.html). -```js +```jsx {({ state }) => ( ...