Skip to content

Commit

Permalink
Update README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
collardeau committed Apr 3, 2018
1 parent 7f2ea7a commit 17058f0
Showing 1 changed file with 51 additions and 3 deletions.
54 changes: 51 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,61 @@

A tiny state machine designed for React

# Example
# Install

`npm install staterize --save`

# Usage

Example with React:

```javascript

const state = {
count: 0
};
const deriveState = [
{
isBinary: st => st.count === 0 || st.count === 1
}
];

class App extends React.Component<any, any> {
// init a store
store = staterize(state, deriveState, st => {
// callback when state changes
this.setState(st);
});
state = this.store(); // no params gets current state from store
incr() {
// use store to make state changes
this.store({
count: this.state.count + 1
// no need to update isBinary!
});
render() {
const { count, isBinary } = this.state;
return (
<div>
{count}
isBinary: {isBinary ? 'yes' : 'no'}
<button onClick={this.incr} />
</div>
);
}
}
}

```

Stand-alone example:


```javascript
import staterize from 'staterize';

// prepare your state and its derivations:

const state = {
count: 0
};
Expand All @@ -25,6 +72,7 @@ import staterize from 'staterize';
];

// what to do when the state is updated:

const onStateChange = st => {
console.log(st)
}
Expand All @@ -38,8 +86,8 @@ import staterize from 'staterize';

// calling store with some state updates:
store({ count: 10 })
// triggers the onStateChange callback with:
// triggers the onStateChange callback with the derived state:
{ count: 10, is10: true)

```
More examples coming soon...

0 comments on commit 17058f0

Please sign in to comment.