Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

89
Vistas
In React Js, Want to render component only if props updated

I want to render the component if its props are updated others don't need to render. But in my code, I can see it's rendering with unexpected behavior.

React Version 17.X.X

See below code

index.js

const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(
  <React.StrictMode>
    <App />
  </React.StrictMode>
);

app.js file

import React, { useState, useCallback} from 'react'
import ListItem from './ListItem'
function App() {
  const [itemList, setItemList] = useState([]);
  const [counter, setCounter] = useState(0);
  
  const increase = useCallback(() => setCounter(prevState => prevState + 1), counter); 

  return (
    <div className="App">
      <ListItem itemList={itemList} addItem={(val) => setItemList(prev => [...prev,val])}/>
      <button onClick={increase}> Increament Count </button>
    </div>
  );
}

export default App;

ListItem.js

import React, {memo} from 'react';
const ListItem = memo(props) => {
 console.log('listItem render...');
 return (
  <div>{props.itemList.map((c) => <span>{c}</span>)}</div>
  <button onClick={() => props.addItem(props.itemList.length + 1)}>+ Add Item</button>
 )
}

export default ListItem;

You can see my code and now I want to understand how I can avoid unexpected rendering. Because useCallback using I can avoid re-initiation of that method. but the ListItem component if you can see it's rendering Even if you are adding counter value.

Let me know if you have any questions.

Thanks for the help.

about 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

It is because addItem has a method type prop so you need to create a useCallback() for that method which you have passed as a prop in addItem. This will work for you:


const onAddItem = useCallback((val) => {
  setItemList(prev => [...prev,val]);
}, [itemList]);

<ListItem itemList={itemList} addItem={onAddItem}/>
about 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda