Ritu Singh
Problem:
I am learning react and I am building an application that has a json array in a state variable. Since react doesn't seem to re-render the page of a json array state variable with out making a copy of it first as >mentioned here . Would it be more performant to just have a state variable that I can update anytime I want the page to re render. By doing this would I improve performance?
Example code making a hero tracking app
vs using updatePage stateChange to rerender the page.
Since updateState is just a boolean would it be more efficient to update state like this?
Which of these would be more efficient and how can I test to see which one performs faster?
Solution:
At a high level, both of your implementations are going to have the same performance. Whenever you add a new hero to your list, React still has to re-render that list by iterating over the array, so your time complexity is O(n). That fact doesn't change regardless of how you want to update the page.
Given that there is no way to avoid iterating over your list to re-render it after an update, I would suggest avoiding your second approach. useState(Data.heroes) is perfect for cases like this -- it will only trigger a re-render when the stateful value updates, and it handles that all by itself. Trying to control when the render happens is only adding additional complexity for you and future versions of yourself trying to maintain this code.
And, as unrelated advice, I'd recommend not worrying too much about performance when you're starting out on an app. Get it working first, then optimize it. Happy hacking! :)
Suggested blogs:
>Fix the python-decouple issue in Django
>Address the N+1 problem in Django with prefetch
>Know everything about AWS Resource Explorer: Full Guide
>Reasons why you cannot ping your AWS EC2 instance: How to fix it
>AWS API Gateway: Ways to do REST API Versioning
>4 easy ways to select an AWS Profile when using Boto3 to connect to CloudFront
>How to use Wildcards to ‘cp’ a File Group with AWS CLI