question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Redux chrome devtools crashing if bound action creator is not wrapped in another function

See original GitHub issue

I came across a strange issue while dispatching actions that have no payload when I have the chrome devtools enabled.

The below code won’t work with devtools-enabled in the store:

Store creation

import { createStore, combineReducers } from 'redux';
import dummyReducer from '../ducks/dummy';

const reducers = combineReducers({
  dummy: dummyReducer,
});

const store = createStore(
  reducers,
  window.__REDUX_DEVTOOLS_EXTENSION__ && window.__REDUX_DEVTOOLS_EXTENSION__()
);

export default store;

Dummy duck

import { createAction, handleActions } from 'redux-actions';

// Actions
const DUMMY_TOGGLE = 'glen-checkout/DUMMY_TOGGLE';

// Reducer
const initalState = {
  isDummy: false,
};

export default handleActions({
  [DUMMY_TOGGLE]: state => ({ isDummy: !state.isDummy }),
}, initalState);

// Action Creators
export const toggleDummy = createAction(DUMMY_TOGGLE);

Dummy Connected component

import React, { PropTypes } from 'react';
import { connect } from 'react-redux';
import { toggleDummy } from '../ducks/dummy';

const DummyComp = ({ isDummy, onToggleDummy }) => (
  <div style={{ marginTop: 30 }}>
    <button style={{ marginRight: 30 }} onClick={onToggleDummy}>Toggle</button>
    <span>{isDummy ? 'Oh ma\'h, I am a dummy component! 🐗 ' : 'I am a clever component 🤓 '}</span>
  </div>
);

DummyComp.propTypes = {
  isDummy: PropTypes.bool.isRequired,
  onToggleDummy: PropTypes.func.isRequired,
};

export default connect(
  state => ({
    isDummy: state.dummy.isDummy,
  }),
  {
    onToggleDummy: toggleDummy,
  }
)(DummyComp);

This component won’t be able to dispatch an action if I set onClick={onToggleDummy}. The console reports the below error when I click on the toggle button:

image

However if I disable devtools in the store, everything works fine.

That problem can be worked around by wrapping the onClick handler in a extra function: onClick={() => onToggleDummy()}

Any ideas why this could be happening?

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:2
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

5reactions
zalmoxisuscommented, Jan 18, 2017

I don’t seem to reproduce the issue with failing to access toISOString.

However, accessing the synthetic event will still not be possible as described in #275. As we are not allowed to access the object properties without throwing an Error by React (in dev mode only), we cannot detect if this object is an event. So, the only solution to prevent this, would be to check if it’s an instance of SyntheticEvent like so:

import SyntheticEvent from 'react/lib/SyntheticEvent';
// ...

const store = createStore(rootReducer, window.__REDUX_DEVTOOLS_EXTENSION__ && window.__REDUX_DEVTOOLS_EXTENSION__({
  serialize: {
    replacer: (key, value) => {
      if (value && value instanceof SyntheticEvent) return '[Event]';
      return value;
    }
  }
}));

Let me know if this approach works for you.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Redux dev tool crashing when I use this action - Stack Overflow
If the Redux DevTools detect a flow of actions that consume too many resources this will crash because can handle all the "side...
Read more >
React, Redux & Material UI Workshop for Beginners - Skillshare
What you'll learn. Create a Todo app from scratch with React; Setup Redux-Saga in the application; Use Material UI components to build the...
Read more >
React Js Interview Questions and Answers - TryCatch Classes
Binding in Constructor: In JavaScript classes, the methods are not bound by default. ... Redux wrap it in another function that looks like...
Read more >
Learn from 425 web development courses on egghead
Supabase is a collection of open-source tools that wrap around a PostgreSQL database. In this course, we look at building a realtime chat...
Read more >
Is redux really a good idea? : r/reactjs - Reddit
Yeah, some might not ask for immutability and some may have a different way to write action object, but other than that everything...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found