You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: README.md
+43-25
Original file line number
Diff line number
Diff line change
@@ -20,19 +20,19 @@ Most of the time, I see colleagues starting React projects setting up Redux + a
20
20
21
21
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).
22
22
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.
24
24
25
25
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.
26
26
27
-
## The solution
27
+
## This solution
28
28
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.
30
30
31
31
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.
32
32
33
33
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.
34
34
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.
36
36
37
37
## Table of Contents
38
38
@@ -72,52 +72,67 @@ You can find the library on `window.statty`.
72
72
## Usage
73
73
74
74
```jsx
75
+
// https://codesandbox.io/s/rzpxx0w34
75
76
importReactfrom'react'
76
77
import { render } from'react-dom'
77
78
import { Provider, State } from'statty'
79
+
importinspectfrom'statty/inspect'
78
80
79
-
// selector
81
+
// selector is a function that returns a slice of the state
82
+
// if not specified it defaults to f => f
80
83
constselector=state=> ({ count:state.count })
81
84
82
85
// updaters
83
86
84
-
//only returns the slice of the state supposed to be updated
85
-
//new state will be shallowly merged with old state
86
-
constdec=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
// 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
+
constApp= () => (
121
+
<Provider state={initialState} inspect={inspect}>
109
122
<Counter />
110
123
</Provider>
124
+
)
111
125
112
126
render(<App />, document.getElementById('root'))
113
127
128
+
114
129
```
115
130
116
131
The `<Provider>` component is used to share the state via context.
117
132
The `<State>` component takes 2 props:
118
133
119
134
*`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.
121
136
122
137
State updates happen via special `updater` functions that take the old state as a parameter and return the new state, triggering a rerender.
123
138
@@ -173,19 +188,22 @@ A render prop that takes the state returned by the `selector` and an `update` fu
173
188
174
189
## Examples
175
190
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):
179
192
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).
0 commit comments