-
-
Notifications
You must be signed in to change notification settings - Fork 3.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Request: pass own props to areStatesEqual
#781
Comments
I think you're looking too high up in the tree. |
/*
const state = {
users : {
1 : { ... },
2 : { ... }
}
}
*/
type UserComponentProps = {
user : User
};
function UserComponent({ user } : UserComponentProps) {
return <div>
{/* snip */}
</div>
}
type UserContainerProps = {
id : number
}
function mapStateToProps(state, ownProps : UserContainerProps) {
// assume `getUserFromState` is expensive
return getUserFromState(state, ownProps.id);
}
const UserContainer = connect(mapStateToProps)(UserComponent); Now suppose I have a
If instead I could define a function |
I'd argue that anything in your mapStateToProps shouldn't be expensive. I would work to make that easier to query by doing some of the work upfront in your reducer. Or you can use something like reselect (react-redux uses it internally, so you already have it in your project) to make it cacheable. |
Nitpicking a bit: React-Redux does not depend on Reselect. All of |
Oh yeah, duh. I still hang on to that because of Jim's initial implementation for some reason. |
Could please someone reconsider? This feature feels half baked without ownProps support and there is obviously a demand for it, even the code change is very minor (see #865). @TiddoLangerak is giving the root reason, ownProps allows to reuse the same component with different state slices, it's a basic requirement for any non trivial redux app. |
The change is less then 1 line of code and makes the areStatesEqual way more flexible if nextOwnProps and ownProps arguments are available to control and implement your custom rerender prevention strategy. Since adding those to 2 arguments is completely backwards compatible, I also would like to see this PR merged... Can this again be reconsidered? Thanks! |
Any update on this? this has many upvotes and I have a similar case where this is needed. |
@timdorr : thinking about this a bit, I don't see any overriding reason why we can't or shouldn't implement passing |
Would we be passing nextOwnProps? I believe that's right. Seems easy enough. |
@timdorr / @markerikson mind taking a look at #1947? There's definitely been interest in adding this functionality and it sounds like you aren't opposed to it. Allowing this enables the ability for much more fine-grained checks to be performed in areStatesEqual (decreasing the false negatives). |
I want to implement
areStatesEqual
in such a way that it only compares the relevant state for a container. However, this relevant state might be dependent on the container's own props. E.g. suppose my state looks like this:and I have a container
<User id={userId}>
. Currently, I can't access the id from theareStatesEqual
function, and as such the best I can do is compare the entireusers
object. This gives a lot more false positives though, and is generally more expensive as well. If I had access to the properties of the container then I could make a much more efficientareStatesEqual
function.The text was updated successfully, but these errors were encountered: