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.

Using `connect()` with `Immutable.Map` state object

See original GitHub issue

My store is an Immutable.Map object, and I would like to pass it down my tree of React components. I suppose that in most cases I can get good performance by using something like PureRenderMixin (or ES2015 equivalent) when doing so.

I started out simple decorating my App class with @connect(state => state), and I got this error:

“Uncaught Error: Invariant Violation: mapStateToProps must return an object. Instead received Map {…”

I’m able to do something like @connect(state => {return {state};}), and then unwrap the object in App’s render(), but I get the sense that this is not an ideal solution.

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:18 (8 by maintainers)

github_iconTop GitHub Comments

29reactions
gaearoncommented, Aug 17, 2015

Indeed, as @gnoff said above, you can’t just return Immutable object because we spread over the values you return, and this wouldn’t make sense with Immutable.

I’m able to do something like @connect(state => {return {state};}), and then unwrap the object in App’s render(), but I get the sense that this is not an ideal solution.

The idiomatic way is to do this:

connect(state => ({
  stuff: state.get('stuff'),
  otherStuff: state.get('otherStuff')
})

only for the fields you actually care about, so @connect can bail out when the parts the component doesn’t need, did not change. I don’t suggest using toJS() because AFAIK it’s deep, but deep conversions won’t be as performant as just extracting the stuffy you want.

I also consider using several @connect() a bit higher up the tree, instead of one at the root component.

This is the way to go, and it’s more performant than just one @connect at the top.

3reactions
gaearoncommented, Aug 17, 2015

btw if you’re concerned about performance, make sure you check out reselect and Computing Derived Data.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Connect: Extracting Data with mapStateToProps - React Redux
The first argument to a mapStateToProps function is the entire Redux store state (the same value returned by a call to store.getState() )....
Read more >
Using an ImmutableJS Object as the React Component State
When you call setState(), React merges the object you provide into the current state. So after setState you'll be left with an empty...
Read more >
React, Redux, and Immutable.js: Ingredients for Efficient UI
Is it really necessary to use Immutable.js? According to official documentation reducer function in Redux should always emit a new state object using...
Read more >
Using Immutable.JS with React and Redux - Fullstack.io
A guide on when to use Immutable.js and how to use it with Redux. ... If you use Map() or Set() from the...
Read more >
Immutability in React and Redux: The Complete Guide
Update an item in an array with map; Update an object in an array; Remove an item from an array with filter. Easy...
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