0

I have a component that renders a Datatable from primereact. I am loading the data from the server, and I am setting an offset state in the main component to manage the pagination. Everything is good, but when I try to change the page in the Datatable it changes, but it quickly resets itself to its initial state.

enter image description here

You can see that the offset always returns to value 0 when it changes. That happends only by clicking the next page in the Datatable.

import React, { useState } from 'react';
import { useQuery } from '@apollo/client';
import '../../layout/customs/responsiveDatatable.scss';
import { clientAuthApiKey as client } from '../../client';
import { query } from '../../graphql/queries';
import ClientsTable from './ClientsTable';

function Clientes() {
    const [searchInput] = useState('');
    const [offset, setOffset] = useState(0);

    const queryParams = {
        client,
        variables: {
            input: {
                pagination: { limit: 10, offset },
                search: {
                    searchFields: ['name', 'email', 'document_id'],
                    searchTerm: searchInput || '',
                },
                orderBy: { field: 'name', direction: 'ASC' },
            },
        },
    };

    const { data, loading } = useQuery(query, queryParams);

    const { items: clientes = [], total = 0 } = data?.clients || {};

    console.log({ offset, loading, total });

    return (
        <div className=' p-grid growable p-container'>
                <ClientsTable
                    loading={loading}
                    value={clientes}
                    emptyMessage='Error message'
                    rows={10}
                    total={total}
                    first={offset}
                    onPage={({ first }) => setOffset(first)}
                />
        </div>
    );
}

export default Clientes

The Datatable:

import React from 'react';
import { DataTable } from 'primereact/datatable';
import { Column } from 'primereact/column';

const ClientsTable = ({
    loading,
    value,
    emptyMessage,
    rows,
    total,
    onPage,
    first,
}) => (
        <DataTable
            totalRecords={total}
            loading={loading}
            value={value}
            paginator
            lazy
            paginatorTemplate='FirstPageLink PrevPageLink PageLinks NextPageLink LastPageLink'
            emptyMessage={emptyMessage}
            rows={rows}
            onPage={onPage}
            first={first}
        >
            <Column
                body={(rowData) => `${rowData.name} ${rowData.surname}`}
                header='name'
                headerStyle={{ width: '14em' }}
            />
            <Column
                field='email'
                header='email'
                headerStyle={{ width: '14em' }}
            />
            <Column
                body={() => `Activa`}
                header='State'
                headerStyle={{ width: '7em' }}
            />
        </DataTable>
    );

export default ResponsiveDataTable;

Any idea why is the offset state resetting? Thank You for all the advices.

0 Answers0