Skip to content

Commit 96e041c

Browse files
author
Cassio Zen
committed
Updated Readme
1 parent 609ba3a commit 96e041c

File tree

1 file changed

+17
-11
lines changed

1 file changed

+17
-11
lines changed

README.md

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -54,30 +54,30 @@ console.log(state); // { value: 'active', nextEvents: ['TOGGLE'] }
5454

5555
## What's up with the double parenthesis?
5656

57-
useStateMachine is a curried function because TypeScript doesn't yet support [partial gerenics type inference](https://github.com/microsoft/TypeScript/issues/14400).
57+
useStateMachine is a curried function because TypeScript doesn't yet support [partial generics type inference](https://github.com/microsoft/TypeScript/issues/14400).
5858
This work around allows TypeScript developers to provide a custom type for the context while still having TypeScript infer all the types used in the configuration (Like the state & transitions names, etc...).
5959

60-
## API
60+
# API
6161

62-
### useStateMachine
62+
## useStateMachine
6363

6464
```typescript
6565
const [state, send] = useStateMachine(/* Optional Context */)(/* Configuration */);
6666
```
6767

6868
`useStateMachine` takes a JavaScript object as context (optional, see below) and one as the state machine configuration. It returns an array consisting of a `current state` object and a `transition` function.
6969

70-
**`current state`**
70+
**state**
7171

72-
The `current state` consists of three properties: `value`, `nextEvents` and `context`.
72+
The `state` consists of three properties: `value`, `nextEvents` and `context`.
7373

7474
`value` returns the name of the current state. `nextEvents` returns an array with the names of available transitions from this state.
7575

76-
**`transition`**
76+
**transition**
7777

7878
`transition` takes a transition name as argument. If the transition exists and is allowed (see guard), it will change the state machine state and execute effects.
7979

80-
### State Machine configuration
80+
## State Machine configuration
8181

8282
We'll pass the machine configuration as the second argument to useStateMachine:
8383

@@ -100,7 +100,7 @@ const [state, send] = useStateMachine()({
100100
});
101101
```
102102

103-
#### Transition Syntax
103+
### Transition Syntax
104104

105105
For each state, you can define the possible transitions.
106106

@@ -122,7 +122,7 @@ on: {
122122
};
123123
```
124124

125-
#### Effects
125+
### Effects
126126

127127
Uses the same format as useEffect: Effects are triggered when the state machine enters a given state. If you return a function from your effect, it will be invoked when leaving that state.
128128

@@ -141,7 +141,7 @@ const [state, send] = useStateMachine()({
141141
});
142142
```
143143

144-
#### Guards
144+
### Guards
145145

146146
You can set up a guard per transition, using the transition object syntax. Guard run before actually running the transition: If the guard returns false the transition will be denied.
147147

@@ -166,7 +166,7 @@ const [state, send] = useStateMachine()({
166166
});
167167
```
168168

169-
#### Extended state (context)
169+
### Extended state (context)
170170

171171
Besides the finite number of states, the state machine can have extended state (known as context).
172172

@@ -213,3 +213,9 @@ const [state, send] = useStateMachine<{ toggleCount: number }>({ toggleCount: 0
213213
},
214214
});
215215
```
216+
217+
## About the codebase
218+
219+
This library is build on top of React's useReducer and useEffect hooks. This video walks through creating a basic version of this library:
220+
221+
[![Creating a custom State Machine Hook with useReducer & useEffect](https://img.youtube.com/vi/jF1tO2hTdC0/0.jpg)](https://youtu.be/jF1tO2hTdC0)

0 commit comments

Comments
 (0)