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

368
Vistas
React: infinite render on useEffect only when using an internal component

I have this following code Parent and DataFilter.

Parent.js

const Parent = () => {
  const [filteredData, setFilteredData] = useState()

  ...

  const Filter = () => {
    return (
      <div>
        <h2>Filter your data</h2>
        <DataFilter data={filteredData} updateFilter={setFilteredData} />
        <Result data={filteredData} />
      </div>
    )
  }


  return (
    <div>
      <h1>Header</h1>
      <Filter />
    </div>
  )
}

DataFilter.js

const DataFilter = ({ data, updateFilter }) => {
  const [name, setName] = useState(defaultFilters.name)
  const [age, setAge] = useState(defaultFilters.age)
  
    ...(filter logic using data)
  
  useEffect(() => {
    updateFilter({ name, age })
  }, [name, age])
}

Here, I receive Maximum update depth exceeded aka infinite rendering, which I kind of know why.

  1. Parent and DataFilter get rendered
  2. DataFilter triggers Parent's setFilteredData in useEffect
  3. Parent gets rerendered because of setFilteredData
  4. Parent being rerendered triggers child component DataFilterrerender
  5. Repeat from No.2

But, this only started happening only after I extracted Filter in 'Parent' as an inner component... I know it's weird but it was working fine when Parent was like this


const Parent = () => {
  const [filteredData, setFilteredData] = useState()

  ...

  return (
    <div>
      <h1>Header</h1>
        <div>
          <h2>Filter your data</h2>
          <DataFilter data={filteredData} updateFilter={setFilteredData} />
          <Result data={filteredData} />
        </div>
    </div>
  )
}  

Does anyone know why the infinite rendering happens only when it's as an inner component?

I tried using useCallback for setFilteredData but it didn't work.

Also what should I do to prevent the infinite render if I want to keep it as a component rather than directly writing everything in a return()?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Your reasoning is correct from what I can gather.

To rephrase:

The child components triggers the parent component's setFilteredData.

When state changes in the parent component the parent will re-render it's children, which will cause the <DataFilter> (along with other children) to be re-rendered, which causes the useEffect to run again since useEffect runs on every render.

This process repeats, causing the parent's state to update and again re-render. And this continues in an infinite loop.

The reason it worked with with your previous structure I believe was due to the nesting of the components, with the parents changes only updating relevant children (as opposed to the longer chain of nesting like your current setup).

If you want it not to always re-render, I'd either keep the relevant logic in the child so the child is responsible for only its own state and re-renders, instead of the parent state changes causing the child to re-render.

Alternatively, I would look at wrapping the child in React.memo HOC which will limit re-renders depending on the props.

about 4 years ago · Juan Pablo Isaza 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