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
* Update README.md
Some suggestions. In *Getting Started* I would also mention `RePureComponent` and explain how to also pass a `payload` with actions right away. People usually look for `TL;DR` :D
* Update README.md
* Format README
Copy file name to clipboardExpand all lines: README.md
+21-16Lines changed: 21 additions & 16 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -26,9 +26,13 @@
26
26
27
27
---
28
28
29
-
There are numerous solutions to managing state in React including powerful libraries like [Redux][] and architectures like [Flux] which evolves around the concept of a "reducer": A single handler for all actions in one store. Recent development of [Reason][] builds upon this concepts and integrates it tightly into [ReasonReact][]by the use of a "reducer component".
29
+
A number of solutions to manage state in React applications are based on the concept of "reducer" – a function that transforms the state of the application in response to actions. An example is the [Redux][]library and architectures like [Flux][].
30
30
31
-
A reducer component is used like a regular, stateful, React component with the added twist that `setState` is not allowed. Instead, state is updated through a `reducer` which is triggered by sending actions to it. ReComponent brings this concept to your React application today.
31
+
Most recently this pattern was implemented in [ReasonReact][] as the built-in solution to manage local component state. Similarly to Redux, ReasonReact components implement a reducer and actions to trigger state changes. Stateful components are usually referred as "reducer component".
32
+
33
+
_ReComponent_ borrows these ideas from ReasonReact and brings reducer components to the React ecosystem.
34
+
35
+
A reducer component is used like a regular, stateful, React component with the added twist that `setState` is not allowed. Instead, state is updated through a `reducer` which is triggered by sending actions to it.
Using `ReComponent` works in the same way as using a regular React component. State is usually initialized using the `initialState()` callback and can only be modified by sending actions to the `reducer()` function. To help with that, you can use `createSender()`. Take a look at a simple counter example:
54
+
To create a reducer component extend `ReComponent` from `react-recomponent` instead of `React.Component`.
55
+
56
+
With `ReComponent` state is usually initialized using the `initialState()` callback and can only be modified by sending actions to the `reducer()` function. To help with that, you can use `createSender()`. Take a look at a simple counter example:
51
57
52
58
```js
53
59
importReactfrom"react";
@@ -84,28 +90,28 @@ class Counter extends ReComponent {
The `Counter` component starts with an initial state of `{ count: 0 }`. Note that this state is in fact a regular React component state. To update it, we use a click action which we identify by its type `"CLICK"` (This is similar to the way we actions are identified in Redux).
93
+
The `Counter` component starts with an initial state of `{ count: 0 }`. Note that this state is in fact a regular React component state. To update it, we use a click action which we identify by its type `"CLICK"` (this is similar to the way actions are identified in Redux).
88
94
89
95
The `reducer` will receive this action and act accordingly. In our case, it will return an `Update()` effect with the modified state.
90
96
91
97
ReComponent comes with four different types of [effects](https://github.com/philipp-spiess/react-recomponent#effects):
92
98
93
-
-`NoUpdate()`to signalize that nothing should happen.
94
-
-`Update(state)`to update the state.
95
-
-`SideEffects(fn)`to run an arbitrary function to issue a [side effect]. Side effects may never be run directly inside the reducer. A reducer should always be pure: For the same action applied onto the same state, it should return the same effects. This paves the way to future react features like async React and make it easier to maintain your code.
96
-
-`UpdateWithSideEffects(state, fn)`to do both: update the state and then trigger the side effect.
99
+
-`NoUpdate()` signalize that nothing should happen.
100
+
-`Update(state)` update the state.
101
+
-`SideEffects(fn)` run an arbitrary function which has [side effect]s. Side effects may never be run directly inside the reducer. **A reducer should always be pure**: for the same action applied onto the same state, it should return the same effects. **This is to avoid bugs when React will work asynchronously**.
102
+
-`UpdateWithSideEffects(state, fn)` both update the state and then trigger the side effect.
97
103
98
-
By intelligently returning any of the four types, it is possible to transition between states at one place and without the need to use `setState()` manually which drastically simplifies our mental model: Whatever happens, it must go through the reducer first.
104
+
By intelligently using any of the four types above, it is possible to transition between states in one place and without the need to use `setState()` manually. This drastically simplifies our mental model since changes must always go through the reducer first.
99
105
100
106
## Advanced Usage
101
107
102
108
Now that we‘ve learned how to use reducer components with React, it‘s time to look into more advanced use cases to effectively handle state transitions across bigger portions of your app.
103
109
104
110
### Side Effects
105
111
106
-
We‘ve already heard that ReComponent comes with four different types of [effects](https://github.com/philipp-spiess/react-recomponent#effects). This is necessary to affectively handling side effects by keeping your reducer pure (given the same state and action, it will always return the same effects).
112
+
We‘ve already said that ReComponent comes with four different types of [effects](https://github.com/philipp-spiess/react-recomponent#effects). This is necessary to effectively handle side effects by keeping your reducer pure – given the same state and action, it will always return the same effects.
107
113
108
-
The following example will demonstrate the four different types of effects and shows you how to use them:
114
+
The following example will demonstrate the four different types of effects and show you how to use them:
109
115
110
116
```js
111
117
importReactfrom"react";
@@ -170,9 +176,9 @@ class Counter extends ReComponent {
170
176
171
177
### Handling Events
172
178
173
-
React uses a method called pooling to improve performance when emitting events (check out the guides on [`SyntheticEvent`](https://reactjs.org/docs/events.html) to learn more). This means that events that React will trigger will be reused after the callback was handled.
179
+
React uses a method called pooling to improve performance when emitting events (check out the guides on [`SyntheticEvent`](https://reactjs.org/docs/events.html) to learn more). Basically React recycles events once the callback is handled making any reference to them unavailable.
174
180
175
-
Since the reducer function always runs within the `setState()` callback provided by React, synthetic events will already be recycled. To still be able to access event properties, we recommend passing the required values explicitly. The following example will show the coordinates of the last mouse click. To have control over which properties are sent to the reducer, we‘re using `send` directly in this case:
181
+
Since the reducer function always runs within the `setState()` callback provided by React, synthetic events will already be recycled by the time the reducer is invoked. To be able to access event properties, we recommend passing the required values explicitly. The following example will show the coordinates of the last mouse click. To have control over which properties are sent to the reducer, we‘re using `send` directly in this case:
176
182
177
183
```js
178
184
importReactfrom"react";
@@ -229,9 +235,9 @@ class Counter extends ReComponent {
229
235
230
236
### Manage State Across the Tree
231
237
232
-
You often want to pass state properties down to other children in order for them to behave properly. Some times, however, this tree is very deep and it might be inefficient to go through the whole tree (and thereby updating it) to pass a value down.
238
+
Often times we want to pass state properties to descendants that are very deep in the application tree. In order to do so, the components in between need to pass those properties to their respective children until we reach the desired component. This pattern is usually called [prop drilling](https://blog.kentcdodds.com/prop-drilling-bb62e02cb691)and it is usually what you want to do.
233
239
234
-
Fortunately, React 16.3.0 introduced a new API called [`createContext()`](https://reactjs.org/docs/context.html#reactcreatecontext)which we can leverage to implement this.
240
+
Sometimes, however, the layers in-between are expensive to re-render causing your application to become janky. Fortunately, React 16.3.0 introduced a new API called [`createContext()`](https://reactjs.org/docs/context.html#reactcreatecontext)that we can use to solve this issue by using context to pass those properties directly to the target component and skipping the update of all intermediate layers:
235
241
236
242
```js
237
243
importReactfrom"react";
@@ -430,7 +436,6 @@ Check out the [type definition tests](https://github.com/philipp-spiess/react-re
0 commit comments