GithubHelp home page GithubHelp logo

Comments (18)

gaearon avatar gaearon commented on April 27, 2024 29

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.

from react-redux.

gaearon avatar gaearon commented on April 27, 2024 3

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

from react-redux.

gaearon avatar gaearon commented on April 27, 2024 2

What do you mean by “update”? React only updates DOM when necessary. There is a cost for reconciliation process (which is where it compares the element trees), and shouldComponentUpdate() lets you bail out of this process.

When you use connect() on a component, it is subscribed to the store, and if the calculated props change, it will cause reconciliation on the connected component.

So if you connect a list to the store, and a list renders 100 rows, if the calculated props change after a store update, React will reconcile those 100 row elements. Then it will look at rows themselves. If your row components implement shouldComponentUpdate() you can bail out there. If your row components are connected, the shouldComponentUpdate() implementation we provide will bail out if props are shallowly unequal.

Does this make sense?

from react-redux.

msuperina avatar msuperina commented on April 27, 2024 1

Sorry for commenting a closed issue but I keep coming here when googling for a similar issue.
I spent a lot of time to understand how to integrate Immutable with Redux, even implementing my own version of connect, and finally understood how to use the @connect from 'redux-connect'
I just wanted to understand @Gaeron answer from 17 august 2015

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

It seems to work ( and looking at the shallowEqual code it seems reasonable ) when state.get('stuff') and state.get('otherStuff') are Immutable objects, not plain JS.
Can someone confirm it is the case or am I missing something ?

from react-redux.

bherrmann7 avatar bherrmann7 commented on April 27, 2024 1

Our project uses an Immutable.Map as our store and connect like so,

  App = connect(state=>state.toObject())(App);

Which seems to neatly pass everything from the map into the App. Our entire application has 125 Components and only the 1 connect. Are we using an anti pattern? It seems performant...

from react-redux.

gnoff avatar gnoff commented on April 27, 2024

The return of the mapStateToProps needs to be an object because the object is spread as props to the wrapped component. Your work around is in fact the idiomatic way to use @connect. In this case your wrapped component will have a prop named 'state' which is an Immutable.Map

Now you may want to reconsider passing the entire app state to your connected component however. This will make refactoring your state shape a lot harder when your app is more complicated. Though in simple cases it is fine.

from react-redux.

 avatar commented on April 27, 2024

Ah, I understand.

I already pass what that I consider being the app state. By passing the entire app state, do you mean doing @connect(state => state.toJS())?

I haven’t fully decided whether or not I want the immutable or a plain JavaScript object floating through the component tree, but performance is the number one priority in this particular case. I need to look into how shouldComponentUpdate() and each alternative will work with my data. I also consider using several @connect() a bit higher up the tree, instead of one at the root component.

from react-redux.

gaearon avatar gaearon commented on April 27, 2024

I already pass what that I consider being the app state. By passing the entire app state, do you mean doing @connect(state => state.toJS())?

That would be slow. I suggest you don't do that, and if perf is your main concern, use @connect granularly.

I need to look into how shouldComponentUpdate() and each alternative will work with my data.

Actually @connect already implements a rather decent shouldComponentUpdate so don't worry about it too much.

from react-redux.

 avatar commented on April 27, 2024

Great, thank you both!

from react-redux.

liorbrauer avatar liorbrauer commented on April 27, 2024

Thanks! This really cleared up the same issue I was having.

from react-redux.

thewillhuang avatar thewillhuang commented on April 27, 2024

@gaearon Hey Dan, what are your thoughts of running immutable AND reselect together versus just one or the other?

from react-redux.

gaearon avatar gaearon commented on April 27, 2024

Feel free to use them together. They're solving different problems and can be used complementary.

from react-redux.

patoncrispy avatar patoncrispy commented on April 27, 2024

Wasn't aware connect() implemented a shouldComponentUpdate() which is great to know... is this in the docs somewhere? Is there a suitable way of making props immutable further down the tree to prevent more nested components updating unnecessarily?

from react-redux.

gaearon avatar gaearon commented on April 27, 2024

Wasn't aware connect() implemented a shouldComponentUpdate() which is great to know... is this in the docs somewhere?

This is an implementation detail. We say “performant” in the repo description but I wouldn’t detail the optimizations as they may change.

Is there a suitable way of making props immutable further down the tree to prevent more nested components updating unnecessarily?

Not sure what you mean. Any connected component does a shallow comparison. If its props (including props from store) don’t change, it doesn’t update.

from react-redux.

patoncrispy avatar patoncrispy commented on April 27, 2024

Sorry, I can see how that made little sense. Well, If I have a container (uses connect()) that has an array of components (could be 100+) which may update frequently but sporadically, would it be possible to only update those that changed in some way? Or would it simply be a case of having to redraw them all as it is the Container that uses connect()?

from react-redux.

patoncrispy avatar patoncrispy commented on April 27, 2024

Yes. Thanks for your wisdom, Dan! Just as I think I'm understanding things then 'WHAM!'... something else comes up. :)

from react-redux.

markerikson avatar markerikson commented on April 27, 2024

The actual return value from mapState does need to be a plain object. However, the values within that object can certainly be Immutable.js data structures. So, in that example, if "stuff" was an Immutable Map, and "otherStuff" was an Immutable List, that would be just fine, as long as your actual component is expecting to get a Map and a List, rather than a plain JS object and a plain JS array.

As a side note, there are a number of utilities out there already to integrate Immutable.js into a Redux app. See https://github.com/markerikson/redux-ecosystem-links/blob/master/immutable-data.md#immutableredux-interop for a list of some of them.

from react-redux.

lee1994522 avatar lee1994522 commented on April 27, 2024

@bherrmann7 How about those nested immutable Data?They can't be converted to a plain object by toObject() function.Only by toJS() can we convert all nested immutable data.

from react-redux.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.