Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

multiple fetching after page size #25

Closed
1 task done
asadkhalili opened this issue Jul 23, 2022 · 2 comments
Closed
1 task done

multiple fetching after page size #25

asadkhalili opened this issue Jul 23, 2022 · 2 comments

Comments

@asadkhalili
Copy link

asadkhalili commented Jul 23, 2022

material-react-table version

v0.28

Describe the bug and the steps to reproduce it

Hi,
After change of rows per page, The request is send twice.

const ReactMaterialTable = ({columns, url, params = {}, ...other}) => {
    const tableRef = useRef()
    const [loading, setLoading] = useState(false);
    const [data, setData] = useState([]);
    const [isLoading, setIsLoading] = useState(false);
    const [isRefetching, setIsRefetching] = useState(false);
    const [columnFilters, setColumnFilters] = useState([]);
    const [globalFilter, setGlobalFilter] = useState('');
    const [sorting, setSorting] = useState([]);
    const [pagination, setPagination] = useState({
        pageIndex: 0,
        pageSize: 10,
    });
    const [rowCount, setRowCount] = useState(0);
    const {get} = useApi();


    useEffect(() => {
        const fetchData = async () => {
            if (!data.length) {
                setIsLoading(true);
            } else {
                setIsRefetching(true);
            }

            const params = {
                ...params,
                'page': `${pagination.pageIndex + 1}`,
                'per-page': `${pagination.pageSize}`,
                'filters': JSON.stringify(columnFilters ?? []),
                'globalFilter': globalFilter ?? '',
                'sorting': JSON.stringify(sorting ?? [])
            };

            const response = await get(url, params);
            if (response.status === 200) {
                if (response.data.data) {
                    setData(response.data.data);
                    setRowCount(response.headers['x-pagination-total-count']);
                    setPagination({...pagination, pageSize: response.headers['x-pagination-per-page']});
                    //pageIndex: response.headers['x-pagination-current-page']
                }
            }
            setIsLoading(false);
            setIsRefetching(false);
        };
        fetchData();
        // eslint-disable-next-line react-hooks/exhaustive-deps
    }, [
        columnFilters,
        globalFilter,
        pagination.pageIndex,
        pagination.pageSize,
        sorting,
    ]);

    return (
        <MaterialReactTable
            {...other}
            columns={columns}
            data={data}
            rowCount={rowCount}
            manualFiltering
            manualPagination
            manualSorting
            onColumnFiltersChange={setColumnFilters}
            onGlobalFilterChange={setGlobalFilter}
            onPaginationChange={setPagination}
            onSortingChange={setSorting}
            state={{
                columnFilters,
                globalFilter,
                isLoading,
                pagination,
                showProgressBars: isRefetching,
                sorting,
            }}
            muiTablePaginationProps={{
                labelRowsPerPage: "تعداد در صفحه",
                labelDisplayedRows: function defaultLabelDisplayedRows({from, to, count}) {
                    return `${from}–${to} از ${count !== -1 ? count : `بیشتر از ${to}`}`;
                }
            }}
            localization={tableLocalization}
        />
    )
}

Screenshots or Videos (Optional)

No response

Do you intend to try to help solve this bug with your own PR?

No, because I don't know how

Terms

  • I understand that if my bug cannot be reliably reproduced in a debuggable environment, it will probably not be fixed and this issue may even be closed.
@KevinVandy
Copy link
Owner

I have not been able to reproduce this in my own remote data codesandbox example. This could be a React 18 dev mode thing where useEffects can run twice in strict mode.

@Yov00
Copy link

Yov00 commented Jan 31, 2023

Hi in dev mode useEffect() is executing twice.
Maybe try removing the strict mode and try again.
ReactDOM.createRoot(document.getElementById('root')).render( <React.StrictMode> X <App /> </React.StrictMode> X, )

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants