Skip to content

Commit 969f7c5

Browse files
authored
chore: More examples (#8)
1 parent 46d828e commit 969f7c5

File tree

2 files changed

+43
-25
lines changed

2 files changed

+43
-25
lines changed

prettier.rc .prettierrc

File renamed without changes.

README.md

+43-25
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,19 @@ Most of the time, I see colleagues starting React projects setting up Redux + a
2020

2121
Despite Redux being awesome, [it's not always needed](https://medium.com/@dan_abramov/you-might-not-need-redux-be46360cf367) and it may slow down the process of onboarding new developers, especially if they are new to the React ecosystem (I have often seen colleagues being stuck for hours trying to understand what was the proper way to submit a simple form).
2222

23-
React already comes with a built-in state management mechanism, and the way to change state is called `setState()`. Local component state is just fine in most of the cases.
23+
React already comes with a built-in state management mechanism, `setState()`. Local component state is just fine in most of the cases.
2424

2525
In real world apps we often have app state, and sometimes it becomes annoying to pass it down the entire component tree, along with callbacks to update it, via props.
2626

27-
## The solution
27+
## This solution
2828

29-
`Statty` is meant to manage app-wide states and can be thought of as a simplified version of Redux.
29+
`Statty` is meant to manage app-wide state and can be thought of as a simplified version of Redux.
3030

3131
It [safely](https://medium.com/@mweststrate/how-to-safely-use-react-context-b7e343eff076) leverages context to expose application state to children, along with a function to update it when needed.
3232

3333
The update function acts like Redux dispatch, but instead of an action, it takes an `updater` function as a parameter that returns the new state.
3434

35-
This way it's easy to write testable updaters and to organize them as you prefer, without having to write boilerplate.
35+
This way it's easy to write testable updaters and to organize them as you prefer, without having to write boilerplate code.
3636

3737
## Table of Contents
3838

@@ -72,52 +72,67 @@ You can find the library on `window.statty`.
7272
## Usage
7373

7474
```jsx
75+
// https://codesandbox.io/s/rzpxx0w34
7576
import React from 'react'
7677
import { render } from 'react-dom'
7778
import { Provider, State } from 'statty'
79+
import inspect from 'statty/inspect'
7880

79-
// selector
81+
// selector is a function that returns a slice of the state
82+
// if not specified it defaults to f => f
8083
const selector = state => ({ count: state.count })
8184

8285
// updaters
8386

84-
// only returns the slice of the state supposed to be updated
85-
// new state will be shallowly merged with old state
86-
const dec = state => ({ count: state.count - 1 })
87+
// onDecrement is an updater and returns the updated slice of state
88+
// that will get shallowly merged with the old state
89+
const onDecrement = state => ({ count: state.count - 1 })
8790

88-
// returns a complete new state
89-
const inc = state => Object.assign({}, state, {count: state.count + 1 })
91+
// onDecrement is an updater and returns a complete new state
92+
// it's a pure function like Redux reducers
93+
const onIncrement = state =>
94+
Object.assign({}, state, { count: state.count + 1 })
9095

91-
const Counter = () =>
96+
// Counter uses a <State> component to access the state
97+
// and the update function used to execute state mutations
98+
const Counter = () => (
9299
<State
93100
select={selector}
94-
render={(state, update) =>
101+
render={({ count }, update) => (
95102
<div>
96-
<button onClick={() => update(dec)}>-</button>
97-
{state.count}
98-
<button onClick={() => update(inc)}>+</button>
99-
</div>}
103+
<span>Clicked: {count} times </span>
104+
<button onClick={() => update(onIncrement)}>+</button>{' '}
105+
<button onClick={() => update(onDecrement)}>-</button>{' '}
106+
</div>
107+
)}
100108
/>
109+
)
101110

102111
// initial state
103112
const initialState = {
104113
count: 0
105114
}
106115

107-
const App = () =>
108-
<Provider state={initialState}>
116+
// The <Provider> component is supposed to be placed at the top
117+
// of your application. It accepts the initial state and an inspect function
118+
// useful to log state mutatations during development
119+
// (check your dev tools to see it in action)
120+
const App = () => (
121+
<Provider state={initialState} inspect={inspect}>
109122
<Counter />
110123
</Provider>
124+
)
111125

112126
render(<App />, document.getElementById('root'))
113127

128+
114129
```
115130

116131
The `<Provider>` component is used to share the state via context.
117132
The `<State>` component takes 2 props:
118133

119134
* `select` is a function that takes the entire state, and returns only the part of it that the children will need
120-
* `render` is a [render prop](https://cdb.reacttraining.com/use-a-render-prop-50de598f11ce) that takes the `selected state` and the `update` functions as parameters, giving the user full control on what to render based on props and state.
135+
* `render` is a [render prop](https://cdb.reacttraining.com/use-a-render-prop-50de598f11ce) that takes the `selected state` and the `update` function as parameters, giving the user full control on what to render based on props and state.
121136

122137
State updates happen via special `updater` functions that take the old state as a parameter and return the new state, triggering a rerender.
123138

@@ -173,19 +188,22 @@ A render prop that takes the state returned by the `selector` and an `update` fu
173188

174189
## Examples
175190

176-
For the moment, the only example available is hosted on [codesandbox](https://codesandbox.io/s/o2rq7oJ0z).
177-
178-
It shows simple and more advanced examples with async state mutations.
191+
Examples exist on [codesandbox.io](https://codesandbox.io/search?refinementList%5Btags%5D%5B0%5D=statty%3Aexample&page=1):
179192

180-
More to come.
193+
- [Counter](https://codesandbox.io/s/rzpxx0w34)
194+
- [Preact example without Preact compat (codepen)](https://codepen.io/vesparny/pen/gGgyVN)
195+
- [Counter with reducer](https://codesandbox.io/s/jp9zj98l5w)
196+
- [Counter with async interactions](https://codesandbox.io/s/kxkp47o597) (uses controlled `selectedItem` API).
197+
- [Wikipedia searchbox with RxJS and Ramda](https://codesandbox.io/s/7wx3v8jqqq) (uses controlled `selectedItem` API).
198+
- [Wikipedia searchbox + Downshift integration](https://codesandbox.io/s/pymj32z5kj)
181199

182200
If you would like to add an example, follow these steps:
183201

184-
1) Fork this [codesandbox](https://codesandbox.io/s/o2rq7oJ0z)
202+
1) Fork this [codesandbox](https://codesandbox.io/s/rzpxx0w34)
185203
2) Make sure your version (under dependencies) is the latest available version
186204
3) Update the title and description
187205
4) Update the code for your example (add some form of documentation to explain what it is)
188-
5) Add the tag: statty:example
206+
5) Add the tag: `statty:example`
189207

190208
## Inspiration
191209

0 commit comments

Comments
 (0)