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
A `static` reducer will not only improve the code because it leaves less
room for side effects, it will also make individual actions easier to
test.
Side effects still should have access to the instance (e.g. to access
refs). To enable this, all side effects will now be called with a
reference to the component as the first argument.
Previous, class property reducers will still work until the 1.0.0
release to avoid making a breaking change in a minor version.
Copy file name to clipboardExpand all lines: README.md
+23-13
Original file line number
Diff line number
Diff line change
@@ -36,7 +36,7 @@ A reducer component is used like a regular, stateful, React component with the d
36
36
37
37
-[Installation](#installation)
38
38
-[Getting Started](#getting-started)
39
-
-[Advantages Over `setState`](#advantages-over-setstate)
39
+
-[FAQ](#faq)
40
40
-[Advanced Usage](#advanced-usage)
41
41
-[Side Effects](#side-effects)
42
42
-[Handling Events](#handling-events)
@@ -67,7 +67,7 @@ class Counter extends ReComponent {
67
67
this.state= { count:0 };
68
68
}
69
69
70
-
reducer(action, state) {
70
+
staticreducer(action, state) {
71
71
switch (action.type) {
72
72
case"CLICK":
73
73
returnUpdate({ count:state.count+1 });
@@ -99,7 +99,9 @@ ReComponent comes with four different types of [effects](https://github.com/phil
99
99
100
100
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.
101
101
102
-
## Advantages Over `setState`
102
+
## FAQ
103
+
104
+
### Advantages Over `setState`
103
105
104
106
The advantages are similar to those of [Redux](https://github.com/reduxjs/redux) or really any state management tool:
105
107
@@ -109,6 +111,14 @@ The advantages are similar to those of [Redux](https://github.com/reduxjs/redux)
109
111
110
112
3. Get rid of side effects with **Pure State Transformation**. By keeping your state changes side effect free, you’re forced into writing code that is easier to test (given an action and a state, it must _always_ return the same new state). Plus you can build extended event sourcing features on top of that since you can easily store all actions that where send to your reducers and replay them later (to go back in time and see exactly how an invalid state occurred).
111
113
114
+
### Why is the reducer `static`?
115
+
116
+
To fully leverage all of the advantages outlined above, the reducer function must not have any side effects. Making the reducer `static` will enforce this behavior since you won’t have access to `this` inside the function. We identified three situations that could need `this` inside the reducer:
117
+
118
+
1. You’re about to read class properties. In this case, make sure those properties are properly encapsulated in the state object.
119
+
2. You’re about to write class properties. This is a side effect and should be handled using the `SideEffects(fn)` effect.
120
+
3. You’re accessing a function that is pure by itself. In this case, the function does not need to be a class property but can be a regular module function instead.
121
+
112
122
## Advanced Usage
113
123
114
124
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.
@@ -141,7 +151,7 @@ class Counter extends ReComponent {
141
151
this.state= { count:0 };
142
152
}
143
153
144
-
reducer(action, state) {
154
+
staticreducer(action, state) {
145
155
switch (action.type) {
146
156
case"NO_UPDATE":
147
157
returnNoUpdate();
@@ -202,7 +212,7 @@ class Counter extends ReComponent {
202
212
});
203
213
}
204
214
205
-
reducer(action, state) {
215
+
staticreducer(action, state) {
206
216
switch (action.type) {
207
217
case"CLICK":
208
218
returnUpdate({
@@ -270,7 +280,7 @@ class Container extends ReComponent {
270
280
this.state= { count:0 };
271
281
}
272
282
273
-
reducer(action, state) {
283
+
staticreducer(action, state) {
274
284
switch (action.type) {
275
285
case"CLICK":
276
286
returnUpdate({ count:state.count+1 });
@@ -308,7 +318,7 @@ class UntypedActionTypes extends ReComponent<Props, State> {
@@ -373,7 +383,7 @@ Check out the [type definition tests](https://github.com/philipp-spiess/react-re
373
383
374
384
-`ReComponent`
375
385
376
-
-`reducer(action, state): effect`
386
+
-`static reducer(action, state): effect`
377
387
378
388
Translates an action into an effect. This is the main place to update your component‘s state.
379
389
@@ -402,13 +412,13 @@ Check out the [type definition tests](https://github.com/philipp-spiess/react-re
402
412
403
413
Returning this effect will update the state. Internally, this will use `setState()` with an updater function.
404
414
405
-
-`SideEffects(fn)`
415
+
-`SideEffects(this => mixed)`
406
416
407
-
Enqueues side effects to be run but will not update the component‘s state.
417
+
Enqueues side effects to be run but will not update the component‘s state. The side effect will be called with a reference to the react component (`this`) as the first argument.
408
418
409
-
-`UpdateWithSideEffects(state, fn)`
419
+
-`UpdateWithSideEffects(state, this => mixed)`
410
420
411
-
Updates the component‘s state and _then_ calls the side effect function.
421
+
Updates the component‘s state and _then_ calls the side effect function.The side effect will be called with a reference to the react component (`this`) as the first argument.
"ClassPropertyReducer(...): Class property `reducer` methods are deprecated. Please upgrade to `static` reducers instead: https://github.com/philipp-spiess/react-recomponent#why-is-the-reducer-static"
0 commit comments