How to avoid re-render in react-redux connected component?












0















I have react-redux component SideBar.js which is subscribing to slice of redux state. When the redux state changes, this component gets re-rendered unnecessarily even it does not subscribe to that changing state in redux. Since my state is nested, i do not want to do deep comparisons in shouldComponentUpdate() to avoid re-rendering ... how can i avoid extra re-rendering ?



const mapStateToProps = (state) => {
return {
guestLogin:{
status : state.guestLogin.status,
marketPlace:state.guestLogin.marketPlace,
newsFeed:state.guestLogin.newsFeed
},
accountLogin:{
showMemberPosts:state.accountLogin.showMemberPosts,
newsFeed: state.accountLogin.newsFeed,
marketPlace:state.accountLogin.marketPlace,
userInfo:state.accountLogin.userInfo
},
marketPlace:{
reset:state.marketPlace.reset,
disableFilters:state.marketPlace.disableFilters,
filters:state.marketPlace.filters,


}

};
};

const mapDispatchToProps = dispatch => {
return {
guestMarketPlaceClickHandler :()=>{dispatch(marketPlaceCLickHandlerDispatcher())},
guestNewsFeedClickHandler:()=>{dispatch(newsFeedClickHandlerDispatcher())},
memberMarketPlaceClickHandler:()=>{dispatch(marketPlaceCLickHandlerDispatcher())},
memberNewsFeedClickHandler:()=>{dispatch(newsFeedClickHandlerDispatcher())},
myPostsClickHandler:()=>{dispatch(myPostsClickHandlerDispatcher());},
dispatch:(action)=>{dispatch(action)}
}
};

export default connect(mapStateToProps,mapDispatchToProps)((SideBar));









share|improve this question























  • There is no other way unfortunately. Try to minimize the number of deep comparisons needed to check if the component needs to be rerendered. Even deep comparisons are far less costly versus needlessly rerendering a large component.

    – Shawn Andrews
    Nov 25 '18 at 6:32











  • @ShawnAndrews.... as you can see my slice of state has 10 key value pairs, is it ok to do 10 comparsions in shouldComponentUpdate() ?

    – Manpreet Singh
    Nov 25 '18 at 6:44











  • can you clarify, what part of state does change for which you dont want your component to re-render? can you gice example for such a slice?

    – dee zg
    Nov 25 '18 at 6:46











  • @deezg.. for e.g.. initial full state for marketPlace has key value pair - isLoading : false which become isLoading: true during some dispatch... at this dispatch,, my SideBar.js component also gets re-rendered even it is not subscribed to this key value pair in slice of marketPlace state

    – Manpreet Singh
    Nov 25 '18 at 6:53











  • ten comparisons is reasonable. it is also much better than the alternative of rerendering

    – Shawn Andrews
    Nov 25 '18 at 6:55
















0















I have react-redux component SideBar.js which is subscribing to slice of redux state. When the redux state changes, this component gets re-rendered unnecessarily even it does not subscribe to that changing state in redux. Since my state is nested, i do not want to do deep comparisons in shouldComponentUpdate() to avoid re-rendering ... how can i avoid extra re-rendering ?



const mapStateToProps = (state) => {
return {
guestLogin:{
status : state.guestLogin.status,
marketPlace:state.guestLogin.marketPlace,
newsFeed:state.guestLogin.newsFeed
},
accountLogin:{
showMemberPosts:state.accountLogin.showMemberPosts,
newsFeed: state.accountLogin.newsFeed,
marketPlace:state.accountLogin.marketPlace,
userInfo:state.accountLogin.userInfo
},
marketPlace:{
reset:state.marketPlace.reset,
disableFilters:state.marketPlace.disableFilters,
filters:state.marketPlace.filters,


}

};
};

const mapDispatchToProps = dispatch => {
return {
guestMarketPlaceClickHandler :()=>{dispatch(marketPlaceCLickHandlerDispatcher())},
guestNewsFeedClickHandler:()=>{dispatch(newsFeedClickHandlerDispatcher())},
memberMarketPlaceClickHandler:()=>{dispatch(marketPlaceCLickHandlerDispatcher())},
memberNewsFeedClickHandler:()=>{dispatch(newsFeedClickHandlerDispatcher())},
myPostsClickHandler:()=>{dispatch(myPostsClickHandlerDispatcher());},
dispatch:(action)=>{dispatch(action)}
}
};

export default connect(mapStateToProps,mapDispatchToProps)((SideBar));









share|improve this question























  • There is no other way unfortunately. Try to minimize the number of deep comparisons needed to check if the component needs to be rerendered. Even deep comparisons are far less costly versus needlessly rerendering a large component.

    – Shawn Andrews
    Nov 25 '18 at 6:32











  • @ShawnAndrews.... as you can see my slice of state has 10 key value pairs, is it ok to do 10 comparsions in shouldComponentUpdate() ?

    – Manpreet Singh
    Nov 25 '18 at 6:44











  • can you clarify, what part of state does change for which you dont want your component to re-render? can you gice example for such a slice?

    – dee zg
    Nov 25 '18 at 6:46











  • @deezg.. for e.g.. initial full state for marketPlace has key value pair - isLoading : false which become isLoading: true during some dispatch... at this dispatch,, my SideBar.js component also gets re-rendered even it is not subscribed to this key value pair in slice of marketPlace state

    – Manpreet Singh
    Nov 25 '18 at 6:53











  • ten comparisons is reasonable. it is also much better than the alternative of rerendering

    – Shawn Andrews
    Nov 25 '18 at 6:55














0












0








0








I have react-redux component SideBar.js which is subscribing to slice of redux state. When the redux state changes, this component gets re-rendered unnecessarily even it does not subscribe to that changing state in redux. Since my state is nested, i do not want to do deep comparisons in shouldComponentUpdate() to avoid re-rendering ... how can i avoid extra re-rendering ?



const mapStateToProps = (state) => {
return {
guestLogin:{
status : state.guestLogin.status,
marketPlace:state.guestLogin.marketPlace,
newsFeed:state.guestLogin.newsFeed
},
accountLogin:{
showMemberPosts:state.accountLogin.showMemberPosts,
newsFeed: state.accountLogin.newsFeed,
marketPlace:state.accountLogin.marketPlace,
userInfo:state.accountLogin.userInfo
},
marketPlace:{
reset:state.marketPlace.reset,
disableFilters:state.marketPlace.disableFilters,
filters:state.marketPlace.filters,


}

};
};

const mapDispatchToProps = dispatch => {
return {
guestMarketPlaceClickHandler :()=>{dispatch(marketPlaceCLickHandlerDispatcher())},
guestNewsFeedClickHandler:()=>{dispatch(newsFeedClickHandlerDispatcher())},
memberMarketPlaceClickHandler:()=>{dispatch(marketPlaceCLickHandlerDispatcher())},
memberNewsFeedClickHandler:()=>{dispatch(newsFeedClickHandlerDispatcher())},
myPostsClickHandler:()=>{dispatch(myPostsClickHandlerDispatcher());},
dispatch:(action)=>{dispatch(action)}
}
};

export default connect(mapStateToProps,mapDispatchToProps)((SideBar));









share|improve this question














I have react-redux component SideBar.js which is subscribing to slice of redux state. When the redux state changes, this component gets re-rendered unnecessarily even it does not subscribe to that changing state in redux. Since my state is nested, i do not want to do deep comparisons in shouldComponentUpdate() to avoid re-rendering ... how can i avoid extra re-rendering ?



const mapStateToProps = (state) => {
return {
guestLogin:{
status : state.guestLogin.status,
marketPlace:state.guestLogin.marketPlace,
newsFeed:state.guestLogin.newsFeed
},
accountLogin:{
showMemberPosts:state.accountLogin.showMemberPosts,
newsFeed: state.accountLogin.newsFeed,
marketPlace:state.accountLogin.marketPlace,
userInfo:state.accountLogin.userInfo
},
marketPlace:{
reset:state.marketPlace.reset,
disableFilters:state.marketPlace.disableFilters,
filters:state.marketPlace.filters,


}

};
};

const mapDispatchToProps = dispatch => {
return {
guestMarketPlaceClickHandler :()=>{dispatch(marketPlaceCLickHandlerDispatcher())},
guestNewsFeedClickHandler:()=>{dispatch(newsFeedClickHandlerDispatcher())},
memberMarketPlaceClickHandler:()=>{dispatch(marketPlaceCLickHandlerDispatcher())},
memberNewsFeedClickHandler:()=>{dispatch(newsFeedClickHandlerDispatcher())},
myPostsClickHandler:()=>{dispatch(myPostsClickHandlerDispatcher());},
dispatch:(action)=>{dispatch(action)}
}
};

export default connect(mapStateToProps,mapDispatchToProps)((SideBar));






reactjs redux






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 25 '18 at 6:21









Manpreet SinghManpreet Singh

687




687













  • There is no other way unfortunately. Try to minimize the number of deep comparisons needed to check if the component needs to be rerendered. Even deep comparisons are far less costly versus needlessly rerendering a large component.

    – Shawn Andrews
    Nov 25 '18 at 6:32











  • @ShawnAndrews.... as you can see my slice of state has 10 key value pairs, is it ok to do 10 comparsions in shouldComponentUpdate() ?

    – Manpreet Singh
    Nov 25 '18 at 6:44











  • can you clarify, what part of state does change for which you dont want your component to re-render? can you gice example for such a slice?

    – dee zg
    Nov 25 '18 at 6:46











  • @deezg.. for e.g.. initial full state for marketPlace has key value pair - isLoading : false which become isLoading: true during some dispatch... at this dispatch,, my SideBar.js component also gets re-rendered even it is not subscribed to this key value pair in slice of marketPlace state

    – Manpreet Singh
    Nov 25 '18 at 6:53











  • ten comparisons is reasonable. it is also much better than the alternative of rerendering

    – Shawn Andrews
    Nov 25 '18 at 6:55



















  • There is no other way unfortunately. Try to minimize the number of deep comparisons needed to check if the component needs to be rerendered. Even deep comparisons are far less costly versus needlessly rerendering a large component.

    – Shawn Andrews
    Nov 25 '18 at 6:32











  • @ShawnAndrews.... as you can see my slice of state has 10 key value pairs, is it ok to do 10 comparsions in shouldComponentUpdate() ?

    – Manpreet Singh
    Nov 25 '18 at 6:44











  • can you clarify, what part of state does change for which you dont want your component to re-render? can you gice example for such a slice?

    – dee zg
    Nov 25 '18 at 6:46











  • @deezg.. for e.g.. initial full state for marketPlace has key value pair - isLoading : false which become isLoading: true during some dispatch... at this dispatch,, my SideBar.js component also gets re-rendered even it is not subscribed to this key value pair in slice of marketPlace state

    – Manpreet Singh
    Nov 25 '18 at 6:53











  • ten comparisons is reasonable. it is also much better than the alternative of rerendering

    – Shawn Andrews
    Nov 25 '18 at 6:55

















There is no other way unfortunately. Try to minimize the number of deep comparisons needed to check if the component needs to be rerendered. Even deep comparisons are far less costly versus needlessly rerendering a large component.

– Shawn Andrews
Nov 25 '18 at 6:32





There is no other way unfortunately. Try to minimize the number of deep comparisons needed to check if the component needs to be rerendered. Even deep comparisons are far less costly versus needlessly rerendering a large component.

– Shawn Andrews
Nov 25 '18 at 6:32













@ShawnAndrews.... as you can see my slice of state has 10 key value pairs, is it ok to do 10 comparsions in shouldComponentUpdate() ?

– Manpreet Singh
Nov 25 '18 at 6:44





@ShawnAndrews.... as you can see my slice of state has 10 key value pairs, is it ok to do 10 comparsions in shouldComponentUpdate() ?

– Manpreet Singh
Nov 25 '18 at 6:44













can you clarify, what part of state does change for which you dont want your component to re-render? can you gice example for such a slice?

– dee zg
Nov 25 '18 at 6:46





can you clarify, what part of state does change for which you dont want your component to re-render? can you gice example for such a slice?

– dee zg
Nov 25 '18 at 6:46













@deezg.. for e.g.. initial full state for marketPlace has key value pair - isLoading : false which become isLoading: true during some dispatch... at this dispatch,, my SideBar.js component also gets re-rendered even it is not subscribed to this key value pair in slice of marketPlace state

– Manpreet Singh
Nov 25 '18 at 6:53





@deezg.. for e.g.. initial full state for marketPlace has key value pair - isLoading : false which become isLoading: true during some dispatch... at this dispatch,, my SideBar.js component also gets re-rendered even it is not subscribed to this key value pair in slice of marketPlace state

– Manpreet Singh
Nov 25 '18 at 6:53













ten comparisons is reasonable. it is also much better than the alternative of rerendering

– Shawn Andrews
Nov 25 '18 at 6:55





ten comparisons is reasonable. it is also much better than the alternative of rerendering

– Shawn Andrews
Nov 25 '18 at 6:55












1 Answer
1






active

oldest

votes


















0














Connecting your component to redux makes it a PureComponent. That is, it will do a shallow comparison of the props to decide if it needs to re-render. By nesting your store values in mapStateToProps you are guaranteeing that the shallow comparison will fail.



That is, guestLogin: {...} creates a new object for the value of guestLogin every time.



You can either use reselect or an equivalent solution to create selectors which return the same objects while the state doesn't change, or just make your mapStateToProps shallow, eg..



const mapStateToProps = (state) => {
return {
glStatus : state.guestLogin.status,
glMarketPlace:state.guestLogin.marketPlace,
glNewsFeed:state.guestLogin.newsFeed
alShowMemberPosts:state.accountLogin.showMemberPosts,
alNewsFeed: state.accountLogin.newsFeed,
alMarketPlace:state.accountLogin.marketPlace,
alUserInfo:state.accountLogin.userInfo
mpReset:state.marketPlace.reset,
mpDisableFilters:state.marketPlace.disableFilters,
mpFilters:state.marketPlace.filters,
};
};





share|improve this answer
























  • thanks for helping... do u think if i can break my reducers to more granular state.. that would help ?

    – Manpreet Singh
    Nov 27 '18 at 0:43













  • reducers don't really come into it. After any action is dispatched to the store all mapStateToProps functions will run and merge their results into the component's props. The component will do the shallow comparison with the previous props and render accordingly. Preventing the render is all about making sure that those top-level props objects don't change unnecessarily.

    – lecstor
    Nov 27 '18 at 5:38











  • thanks so mch.. i got ur point...

    – Manpreet Singh
    Nov 27 '18 at 9:49











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53465166%2fhow-to-avoid-re-render-in-react-redux-connected-component%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














Connecting your component to redux makes it a PureComponent. That is, it will do a shallow comparison of the props to decide if it needs to re-render. By nesting your store values in mapStateToProps you are guaranteeing that the shallow comparison will fail.



That is, guestLogin: {...} creates a new object for the value of guestLogin every time.



You can either use reselect or an equivalent solution to create selectors which return the same objects while the state doesn't change, or just make your mapStateToProps shallow, eg..



const mapStateToProps = (state) => {
return {
glStatus : state.guestLogin.status,
glMarketPlace:state.guestLogin.marketPlace,
glNewsFeed:state.guestLogin.newsFeed
alShowMemberPosts:state.accountLogin.showMemberPosts,
alNewsFeed: state.accountLogin.newsFeed,
alMarketPlace:state.accountLogin.marketPlace,
alUserInfo:state.accountLogin.userInfo
mpReset:state.marketPlace.reset,
mpDisableFilters:state.marketPlace.disableFilters,
mpFilters:state.marketPlace.filters,
};
};





share|improve this answer
























  • thanks for helping... do u think if i can break my reducers to more granular state.. that would help ?

    – Manpreet Singh
    Nov 27 '18 at 0:43













  • reducers don't really come into it. After any action is dispatched to the store all mapStateToProps functions will run and merge their results into the component's props. The component will do the shallow comparison with the previous props and render accordingly. Preventing the render is all about making sure that those top-level props objects don't change unnecessarily.

    – lecstor
    Nov 27 '18 at 5:38











  • thanks so mch.. i got ur point...

    – Manpreet Singh
    Nov 27 '18 at 9:49
















0














Connecting your component to redux makes it a PureComponent. That is, it will do a shallow comparison of the props to decide if it needs to re-render. By nesting your store values in mapStateToProps you are guaranteeing that the shallow comparison will fail.



That is, guestLogin: {...} creates a new object for the value of guestLogin every time.



You can either use reselect or an equivalent solution to create selectors which return the same objects while the state doesn't change, or just make your mapStateToProps shallow, eg..



const mapStateToProps = (state) => {
return {
glStatus : state.guestLogin.status,
glMarketPlace:state.guestLogin.marketPlace,
glNewsFeed:state.guestLogin.newsFeed
alShowMemberPosts:state.accountLogin.showMemberPosts,
alNewsFeed: state.accountLogin.newsFeed,
alMarketPlace:state.accountLogin.marketPlace,
alUserInfo:state.accountLogin.userInfo
mpReset:state.marketPlace.reset,
mpDisableFilters:state.marketPlace.disableFilters,
mpFilters:state.marketPlace.filters,
};
};





share|improve this answer
























  • thanks for helping... do u think if i can break my reducers to more granular state.. that would help ?

    – Manpreet Singh
    Nov 27 '18 at 0:43













  • reducers don't really come into it. After any action is dispatched to the store all mapStateToProps functions will run and merge their results into the component's props. The component will do the shallow comparison with the previous props and render accordingly. Preventing the render is all about making sure that those top-level props objects don't change unnecessarily.

    – lecstor
    Nov 27 '18 at 5:38











  • thanks so mch.. i got ur point...

    – Manpreet Singh
    Nov 27 '18 at 9:49














0












0








0







Connecting your component to redux makes it a PureComponent. That is, it will do a shallow comparison of the props to decide if it needs to re-render. By nesting your store values in mapStateToProps you are guaranteeing that the shallow comparison will fail.



That is, guestLogin: {...} creates a new object for the value of guestLogin every time.



You can either use reselect or an equivalent solution to create selectors which return the same objects while the state doesn't change, or just make your mapStateToProps shallow, eg..



const mapStateToProps = (state) => {
return {
glStatus : state.guestLogin.status,
glMarketPlace:state.guestLogin.marketPlace,
glNewsFeed:state.guestLogin.newsFeed
alShowMemberPosts:state.accountLogin.showMemberPosts,
alNewsFeed: state.accountLogin.newsFeed,
alMarketPlace:state.accountLogin.marketPlace,
alUserInfo:state.accountLogin.userInfo
mpReset:state.marketPlace.reset,
mpDisableFilters:state.marketPlace.disableFilters,
mpFilters:state.marketPlace.filters,
};
};





share|improve this answer













Connecting your component to redux makes it a PureComponent. That is, it will do a shallow comparison of the props to decide if it needs to re-render. By nesting your store values in mapStateToProps you are guaranteeing that the shallow comparison will fail.



That is, guestLogin: {...} creates a new object for the value of guestLogin every time.



You can either use reselect or an equivalent solution to create selectors which return the same objects while the state doesn't change, or just make your mapStateToProps shallow, eg..



const mapStateToProps = (state) => {
return {
glStatus : state.guestLogin.status,
glMarketPlace:state.guestLogin.marketPlace,
glNewsFeed:state.guestLogin.newsFeed
alShowMemberPosts:state.accountLogin.showMemberPosts,
alNewsFeed: state.accountLogin.newsFeed,
alMarketPlace:state.accountLogin.marketPlace,
alUserInfo:state.accountLogin.userInfo
mpReset:state.marketPlace.reset,
mpDisableFilters:state.marketPlace.disableFilters,
mpFilters:state.marketPlace.filters,
};
};






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 26 '18 at 13:42









lecstorlecstor

3,4461217




3,4461217













  • thanks for helping... do u think if i can break my reducers to more granular state.. that would help ?

    – Manpreet Singh
    Nov 27 '18 at 0:43













  • reducers don't really come into it. After any action is dispatched to the store all mapStateToProps functions will run and merge their results into the component's props. The component will do the shallow comparison with the previous props and render accordingly. Preventing the render is all about making sure that those top-level props objects don't change unnecessarily.

    – lecstor
    Nov 27 '18 at 5:38











  • thanks so mch.. i got ur point...

    – Manpreet Singh
    Nov 27 '18 at 9:49



















  • thanks for helping... do u think if i can break my reducers to more granular state.. that would help ?

    – Manpreet Singh
    Nov 27 '18 at 0:43













  • reducers don't really come into it. After any action is dispatched to the store all mapStateToProps functions will run and merge their results into the component's props. The component will do the shallow comparison with the previous props and render accordingly. Preventing the render is all about making sure that those top-level props objects don't change unnecessarily.

    – lecstor
    Nov 27 '18 at 5:38











  • thanks so mch.. i got ur point...

    – Manpreet Singh
    Nov 27 '18 at 9:49

















thanks for helping... do u think if i can break my reducers to more granular state.. that would help ?

– Manpreet Singh
Nov 27 '18 at 0:43







thanks for helping... do u think if i can break my reducers to more granular state.. that would help ?

– Manpreet Singh
Nov 27 '18 at 0:43















reducers don't really come into it. After any action is dispatched to the store all mapStateToProps functions will run and merge their results into the component's props. The component will do the shallow comparison with the previous props and render accordingly. Preventing the render is all about making sure that those top-level props objects don't change unnecessarily.

– lecstor
Nov 27 '18 at 5:38





reducers don't really come into it. After any action is dispatched to the store all mapStateToProps functions will run and merge their results into the component's props. The component will do the shallow comparison with the previous props and render accordingly. Preventing the render is all about making sure that those top-level props objects don't change unnecessarily.

– lecstor
Nov 27 '18 at 5:38













thanks so mch.. i got ur point...

– Manpreet Singh
Nov 27 '18 at 9:49





thanks so mch.. i got ur point...

– Manpreet Singh
Nov 27 '18 at 9:49




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53465166%2fhow-to-avoid-re-render-in-react-redux-connected-component%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Create new schema in PostgreSQL using DBeaver

Deepest pit of an array with Javascript: test on Codility

Costa Masnaga