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

352
Vistas
useSelector and useEffect rerender optimization

I have table component with tableRows stored in useState.

Also I have searcher component outside of table component.

When data inside of searcher changes, tableRows updates inside useEffect.

And it works good, but it causes two rerender. And i understand why. First rerender because of useSelector, and the second because useEffect have useSelector value as dependency.

But how to avoid one rerender. I want it to rerenders when tableRows changes, but not when searcher changes.

const CatalogTable: React.FC<CatalogTableProps> = ({rows}) => {
    const [tableRows, setTableRows] = React.useState(rows)
    const searcher = useSelector(getTableSearcher, shallowEqual)

    const getData = async () => {
        const {data} = await CatalogService.getAllCatalogProducts({page: 1, searcher: searcher})
        setTableRows(data.products)
    }


    React.useEffect(() => {
        if(searcher)
            getData()
    }, [searcher])

    return (
        <>
            <div className={styles.defaultTable}>
                <Table
                    headers={headers}
                    label="Products catalog"
                    rows={tableRows}
                    total={total}
                    pagination
                    addButton
                    editButtons
                    searcher
                    getPage={(page: number) => nextPage(page)}
                    type='catalog'
                />
            </div>
        </>
    )
}

export default CatalogTable

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

0

One of the possible solutions is memoization:

const CatalogTable: React.FC<CatalogTableProps> = ({rows}) => {
    const [tableRows, setTableRows] = React.useState(rows)
    const searcher = useSelector(getTableSearcher, shallowEqual)

    const getData = async () => {
        const {data} = await CatalogService.getAllCatalogProducts({page: 1, searcher: searcher})
        setTableRows(data.products)
    }


    React.useEffect(() => {
        if(searcher)
            getData()
    }, [searcher])

    const result = useMemo( () =>
        <>
            <div className={styles.defaultTable}>
                <Table
                    headers={headers}
                    label="Products catalog"
                    rows={tableRows}
                    total={total}
                    pagination
                    addButton
                    editButtons
                    searcher
                    getPage={(page: number) => nextPage(page)}
                    type='catalog'
                />
            </div>
        </>, [tableRows]
    );

  return result;
}

export default CatalogTable

another solution is putting both tableRows and search query inside redux store and update them simultaneously through async middleware.

about 4 years ago · Juan Pablo Isaza Denunciar

0

It re-renders because your state is changing, which is the expected result. This is because tableRows is being set inside the useEffect (there's a setTableRows inside the effect, which depends on the searcher).

Not sure how the component is being used, since you are passing some initial rows in the props and reseting the rows (setTableRows) inside the component, but from your comment seems that you don't really care about changes in the searcher. A solution to accomplish what you want would be to just pass the rows as props and remove the state from the component, in that way your component would only care about the rows and not about the searcher.

Anyway, optimizing re-renders is not the best way to go, unless your rendering is slow. Also, it is safer to define async calls inside the useEffect, this way is easier to assure that all required dependencies are in the dependencies array.

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