Skip to content

Commit 65ae949

Browse files
committed
[Docs] Synthetic events troubleshooting
Fix #275 and #287.
1 parent 11a76da commit 65ae949

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

docs/Troubleshooting.md

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,52 @@ const store = createStore(reducer, preloadedState, compose(
2929
```
3030

3131
Where `batchedSubscribe` is `redux-batched-subscribe` store enhancer.
32+
33+
### It fails to serialize data when [passing synthetic events](https://github.com/zalmoxisus/redux-devtools-extension/issues/275) or [calling an action directly with `redux-actions`](https://github.com/zalmoxisus/redux-devtools-extension/issues/287)
34+
35+
React synthetic event cannot be reused for performance reason. So, it's not possible to serialize event objects you pass to action payloads.
36+
37+
1. The best solution is not to pass the whole event object to reducers, but the data you need:
38+
```diff
39+
function click(event) {
40+
return {
41+
type: ELEMENT_CLICKED,
42+
- event: event
43+
+ value: event.target.value
44+
};
45+
}
46+
```
47+
48+
2. Another solution would be to use `event.persist()` (in the example above) as suggested in [React Docs](https://facebook.github.io/react/docs/events.html#event-pooling), but it will consume RAM while not needed.
49+
50+
3. If you still need to pass it to an action, you can override this key of the stringified payload in your action creator, by adding a custom `toJSON` function (which will be called by the extension before accessing the object):
51+
52+
```diff
53+
function increment(event) {
54+
return {
55+
type: INCREMENT_COUNTER,
56+
event,
57+
+ toJSON: function (){
58+
+ return { ...this, event: '[Event]' };
59+
+ }
60+
};
61+
}
62+
```
63+
Note that it shouldn't be arrow function as we want to have access to the function's `this`.
64+
65+
As we don't have access to the original object, skipping and recomputing actions during hot reloading will not work in this case. So better to use the required value from the event than the whole event object.
66+
67+
4. If you don't want to add `toJSON` to action creators, a solution to prevent this, would be to use `serialize` parameter and to check if it's an instance of `SyntheticEvent` like so:
68+
```js
69+
import SyntheticEvent from 'react/lib/SyntheticEvent';
70+
// ...
71+
72+
const store = createStore(rootReducer, window.__REDUX_DEVTOOLS_EXTENSION__ && window.__REDUX_DEVTOOLS_EXTENSION__({
73+
serialize: {
74+
replacer: (key, value) => {
75+
if (value && value instanceof SyntheticEvent) return '[Event]';
76+
return value;
77+
}
78+
}
79+
}));
80+
```

0 commit comments

Comments
 (0)