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

238
Vistas
How to properly initialise react table based on the server side data? Too many re-renders

I have code like this:

import React, {Component, useState} from "react";
import DataTable from 'react-data-table-component';
import {columns} from "./table-data/data";
import RequestsPagination from "./CustomPagination";

const json = require('../stub/requests-page-1-response.json')

class RequestsPage extends Component {
    constructor(props) {
        super(props);
        this.state = {
            requests: [],
            currentPage: 0,
            elementsPerPage: 10,
            totalElements: undefined
        };

        this.fetchData = this.fetchData.bind(this)
    }

    fetchData() {
        const {currentPage, elementsPerPage} = this.state
        fetch(`/api/requests?page=${currentPage}&size=${elementsPerPage}`)
            .then(response => {
                if (response.status !== 200) {
                    throw {
                        error: {
                            status: response.status
                        }
                    }
                } else {
                    //return response.json()
                    return {}
                }
            })
            .then(pageResponse => {
                this.setState(prevState => ({
                    ...prevState,
                    requests: json.requests,
                    totalElements: json.totalElements
                }))
            })
            .catch(error => {
                console.log(error)
                this.setState(prevState => ({
                    ...prevState,
                    response: error
                }))
            })
    }

    componentDidMount() {
        this.fetchData()
    }

    render() {
        return (
            <div>
                {myPagination.call(this)}
            </div>
        )
    }
}

function myPagination() {
    const {currentPage, elementsPerPage, requests, totalElements} = this.state
    return (
        <DataTable
            title={"Все запросы"}
            columns={columns}
            data={requests}
            pagination
            paginationDefaultPage={currentPage}
            paginationTotalRows={totalElements}
            paginationPerPage={elementsPerPage}
            paginationRowsPerPageOptions={[10, 20, 50]}
        />
    )
}

export default RequestsPage;

I keep getting:

Too many re-renders. React limits the number of renders to prevent an infinite loop.

I have tried to initialize the child component inside the render and now outside, tried several examples - no result. Can someone refer me to proper documentation and a simple explanation?

My goal is to create a react table with the smallest amount of code, i.e. using libraries. Later I plan to do pagination, I want to use the property of DataTable

onChangePage={(event, page) => this.fetchData()}
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Apparently, this didn't work and I switched to React Table library following https://react-table.tanstack.com/docs/examples/pagination-controlled it worked as a charm

Looks like data-table designed for static data when I needed to get from the server per page

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