Prevent Component/PureComponent with styled-components from re-rendering all items after state change












7















After adding styled-components to this example, we've noticed that our list component updates everything when only one item in state is modified.



The list rendering highlights (from React dev-tools) are excessive when adding/removing a single entry. One item is removed/added, then all items get highlighted.



enter image description here



code samples




  • github: https://github.com/chancesmith/reactjs-optimize-list-updates/tree/master/src

  • codesandbox: https://codesandbox.io/s/wn45qw44z5


Here is an example of the right list component (CategorizedList.js)



import styled from "styled-components";

const Item = styled.li`
color: #444;
`;

class CategorizedList extends PureComponent {
render() {
return (
<div>
{this.props.categories.map(category => (
<ul>
<li key={this.props.catStrings[category]}>
{this.props.catStrings[category]}
</li>
<ul>
{this.props.items.map((item, index) =>
item.category === category ? (
<div key={item.label + index}>
<Item>{item.label}</Item>
</div>
) : null
)}
</ul>
</ul>
))}
</div>
);
}
}


Preference



I'd prefer to use PureComponent so that shouldComponentUpdate() gets handled automatically.



Question



How can we make sure only the modified objects in items state are re-rendered?










share|improve this question























  • according to your link doing a check on their props and state is lightning fast compared to the cost of re-rendering each one What kind of performance hit do you expect to take in that list to where you need to take this approach?

    – Ortho Home Defense
    Nov 15 '18 at 20:45
















7















After adding styled-components to this example, we've noticed that our list component updates everything when only one item in state is modified.



The list rendering highlights (from React dev-tools) are excessive when adding/removing a single entry. One item is removed/added, then all items get highlighted.



enter image description here



code samples




  • github: https://github.com/chancesmith/reactjs-optimize-list-updates/tree/master/src

  • codesandbox: https://codesandbox.io/s/wn45qw44z5


Here is an example of the right list component (CategorizedList.js)



import styled from "styled-components";

const Item = styled.li`
color: #444;
`;

class CategorizedList extends PureComponent {
render() {
return (
<div>
{this.props.categories.map(category => (
<ul>
<li key={this.props.catStrings[category]}>
{this.props.catStrings[category]}
</li>
<ul>
{this.props.items.map((item, index) =>
item.category === category ? (
<div key={item.label + index}>
<Item>{item.label}</Item>
</div>
) : null
)}
</ul>
</ul>
))}
</div>
);
}
}


Preference



I'd prefer to use PureComponent so that shouldComponentUpdate() gets handled automatically.



Question



How can we make sure only the modified objects in items state are re-rendered?










share|improve this question























  • according to your link doing a check on their props and state is lightning fast compared to the cost of re-rendering each one What kind of performance hit do you expect to take in that list to where you need to take this approach?

    – Ortho Home Defense
    Nov 15 '18 at 20:45














7












7








7


4






After adding styled-components to this example, we've noticed that our list component updates everything when only one item in state is modified.



The list rendering highlights (from React dev-tools) are excessive when adding/removing a single entry. One item is removed/added, then all items get highlighted.



enter image description here



code samples




  • github: https://github.com/chancesmith/reactjs-optimize-list-updates/tree/master/src

  • codesandbox: https://codesandbox.io/s/wn45qw44z5


Here is an example of the right list component (CategorizedList.js)



import styled from "styled-components";

const Item = styled.li`
color: #444;
`;

class CategorizedList extends PureComponent {
render() {
return (
<div>
{this.props.categories.map(category => (
<ul>
<li key={this.props.catStrings[category]}>
{this.props.catStrings[category]}
</li>
<ul>
{this.props.items.map((item, index) =>
item.category === category ? (
<div key={item.label + index}>
<Item>{item.label}</Item>
</div>
) : null
)}
</ul>
</ul>
))}
</div>
);
}
}


Preference



I'd prefer to use PureComponent so that shouldComponentUpdate() gets handled automatically.



Question



How can we make sure only the modified objects in items state are re-rendered?










share|improve this question














After adding styled-components to this example, we've noticed that our list component updates everything when only one item in state is modified.



The list rendering highlights (from React dev-tools) are excessive when adding/removing a single entry. One item is removed/added, then all items get highlighted.



enter image description here



code samples




  • github: https://github.com/chancesmith/reactjs-optimize-list-updates/tree/master/src

  • codesandbox: https://codesandbox.io/s/wn45qw44z5


Here is an example of the right list component (CategorizedList.js)



import styled from "styled-components";

const Item = styled.li`
color: #444;
`;

class CategorizedList extends PureComponent {
render() {
return (
<div>
{this.props.categories.map(category => (
<ul>
<li key={this.props.catStrings[category]}>
{this.props.catStrings[category]}
</li>
<ul>
{this.props.items.map((item, index) =>
item.category === category ? (
<div key={item.label + index}>
<Item>{item.label}</Item>
</div>
) : null
)}
</ul>
</ul>
))}
</div>
);
}
}


Preference



I'd prefer to use PureComponent so that shouldComponentUpdate() gets handled automatically.



Question



How can we make sure only the modified objects in items state are re-rendered?







reactjs styled-components react-dom






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 15 '18 at 20:31









Chance SmithChance Smith

387213




387213













  • according to your link doing a check on their props and state is lightning fast compared to the cost of re-rendering each one What kind of performance hit do you expect to take in that list to where you need to take this approach?

    – Ortho Home Defense
    Nov 15 '18 at 20:45



















  • according to your link doing a check on their props and state is lightning fast compared to the cost of re-rendering each one What kind of performance hit do you expect to take in that list to where you need to take this approach?

    – Ortho Home Defense
    Nov 15 '18 at 20:45

















according to your link doing a check on their props and state is lightning fast compared to the cost of re-rendering each one What kind of performance hit do you expect to take in that list to where you need to take this approach?

– Ortho Home Defense
Nov 15 '18 at 20:45





according to your link doing a check on their props and state is lightning fast compared to the cost of re-rendering each one What kind of performance hit do you expect to take in that list to where you need to take this approach?

– Ortho Home Defense
Nov 15 '18 at 20:45












1 Answer
1






active

oldest

votes


















1














If the data changes , the view will re-render. It shouldn't be an expensive process since it happens once on add/remove action. If you find performance issues it might be caused from something else.



In general this would be the way you can have some control on pure-components re-render:
https://reactjs.org/docs/react-api.html#reactmemo






share|improve this answer























    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%2f53327479%2fprevent-component-purecomponent-with-styled-components-from-re-rendering-all-ite%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









    1














    If the data changes , the view will re-render. It shouldn't be an expensive process since it happens once on add/remove action. If you find performance issues it might be caused from something else.



    In general this would be the way you can have some control on pure-components re-render:
    https://reactjs.org/docs/react-api.html#reactmemo






    share|improve this answer




























      1














      If the data changes , the view will re-render. It shouldn't be an expensive process since it happens once on add/remove action. If you find performance issues it might be caused from something else.



      In general this would be the way you can have some control on pure-components re-render:
      https://reactjs.org/docs/react-api.html#reactmemo






      share|improve this answer


























        1












        1








        1







        If the data changes , the view will re-render. It shouldn't be an expensive process since it happens once on add/remove action. If you find performance issues it might be caused from something else.



        In general this would be the way you can have some control on pure-components re-render:
        https://reactjs.org/docs/react-api.html#reactmemo






        share|improve this answer













        If the data changes , the view will re-render. It shouldn't be an expensive process since it happens once on add/remove action. If you find performance issues it might be caused from something else.



        In general this would be the way you can have some control on pure-components re-render:
        https://reactjs.org/docs/react-api.html#reactmemo







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 15 '18 at 20:41









        EranEran

        1,1131225




        1,1131225
































            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%2f53327479%2fprevent-component-purecomponent-with-styled-components-from-re-rendering-all-ite%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

            Florida Star v. B. J. F.

            Danny Elfman

            Lugert, Oklahoma