By using this site, you agree to the Privacy Policy and Terms of Use.
Accept
rocoderesrocoderes
  • Home
  • HTML & CSS
    • Login and Registration Form
    • Card Design
    • Loader
  • JavaScript
  • Python
  • Internet
  • Landing Pages
  • Tools
    • Google Drive Direct Download Link Generator
    • Word Count
  • Games
    • House Painter
Notification Show More
Latest News
How to set the dropdown value by clicking on a table row
Javascript – How to set the dropdown value by clicking on a table row
JavaScript
Attempting to increase the counter, when the object's tag exist
Javascript – Attempting to increase the counter, when the object’s tag exist
JavaScript
Cycle2 JS center active slide
Javascript – Cycle2 JS center active slide
JavaScript
Can import all THREE.js post processing modules as ES6 modules except OutputPass
Javascript – Can import all THREE.js post processing modules as ES6 modules except OutputPass
JavaScript
How to return closest match for an array in Google Sheets Appscript
Javascript – How to return closest match for an array in Google Sheets Appscript
JavaScript
Aa
Aa
rocoderesrocoderes
Search
  • Home
  • HTML & CSS
    • Login and Registration Form
    • Card Design
    • Loader
  • JavaScript
  • Python
  • Internet
  • Landing Pages
  • Tools
    • Google Drive Direct Download Link Generator
    • Word Count
  • Games
    • House Painter
Follow US
High Quality Design Resources for Free.
rocoderes > JavaScript > Javascript – Prevent unchanged children from re-rendering in React
JavaScript

Javascript – Prevent unchanged children from re-rendering in React

Admin
Last updated: 2023/12/13 at 5:57 PM
Admin
Share
2 Min Read
Prevent unchanged children from re-rendering in React

Problem:

I have an intent to write a parent components with an array of children components. It is a kind of classic TODO list. When I type something on child component’s textfield, it triggers the change on parent component, and parent re-renders itself and all the children.

Contents
Problem:Solution:

There are going to be 1000+ of children components in parent, and I don’t want to re-render everything when I type something in one of the children’s textfields.

As an optimization technique I decided to use React.memo on children, so if props of children do not change, they don’t re-render. But right now the state of a parent started to be randomly altered. I’ve spent a good amount of time on this one, but I still struggle to find the culprit.

You can play around with my code in here:

https://codesandbox.io/s/mutable-pine-m6twyv?file=/src/Parent.jsx

Click multiple times on “Add text” button. Then start typing on first textfield, and you will see something strange happening to the list: some items disappear, other’s values get rewritten

Solution:

Your useCallback is missing the [texts] dependency, so all bets are basically off.

Use the functional form of set... to avoid having the dependencies in the first place:

const addText = React.useCallback(() => {
  setTexts(texts => [...texts, "new bla"]);
}, []);

const change = React.useCallback((newText, index) => {
  setTexts(texts => {
    const newTexts = [...texts];
    newTexts[index] = newText;
    return newTexts;
  });
}, []);

(Also, avoid React.memo unless you really do know you need it…)

Related

Subscribe to Our Newsletter

Subscribe to our newsletter to get our newest articles instantly!

Share this Article
Facebook Twitter Email Print
What do you think?
Love0
Sad0
Happy0
Sleepy0
Angry0
Dead0
Wink0
Previous Article transition is not adding up in img created by JavaScript Javascript – transition is not adding up in img created by JavaScript
Next Article Error when uploading a picture Firebase: "TypeError [ERR_INVALID_ARG_VALUE]" Javascript – Error when uploading a picture Firebase: “TypeError [ERR_INVALID_ARG_VALUE]”
Leave a comment Leave a comment

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

- Advertisement -

You Might Also Like

How to set the dropdown value by clicking on a table row

Javascript – How to set the dropdown value by clicking on a table row

February 11, 2024
Attempting to increase the counter, when the object's tag exist

Javascript – Attempting to increase the counter, when the object’s tag exist

February 11, 2024
Cycle2 JS center active slide

Javascript – Cycle2 JS center active slide

February 10, 2024
Can import all THREE.js post processing modules as ES6 modules except OutputPass

Javascript – Can import all THREE.js post processing modules as ES6 modules except OutputPass

February 10, 2024
rocoderesrocoderes
Follow US

Copyright © 2022 All Right Reserved By Rocoderes

  • Home
  • About us
  • Contact us
  • Disclaimer
Join Us!

Subscribe to our newsletter and never miss our latest news, podcasts etc.

Zero spam, Unsubscribe at any time.
Welcome Back!

Sign in to your account

Lost your password?