question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

BootstrapTable doesn't re-render after setState when remote/onTableChange is used

See original GitHub issue

I’m trying to update data using a server call with axios but the table doesn’t update when setState is used inside the onTableChange handler even with basic data. Table data always stays at default assigned state. I got the code from Remote All sample. I’m trying to use this component for first time.

import React, { Component } from "react";
import PropTypes from "prop-types";

import axios from "axios";

import BootstrapTable from "react-bootstrap-table-next";
import paginationFactory from "react-bootstrap-table2-paginator";
import filterFactory, { textFilter } from "react-bootstrap-table2-filter";

const products = [
    { id: 1, name: "test 1", currentVersion: 123 },
    { id: 2, name: "test 2", currentVersion: 234 },
    { id: 3, name: "test 3", currentVersion: 345 }
];

const columns = [
    {
        dataField: "id",
        text: "ID",
        sort: true,
        headerClasses: "text-center",
        classes: "text-center"
    },
    {
        dataField: "name",
        text: "Name",
        filter: textFilter(),
        sort: true
    },
    {
        dataField: "currentVersion",
        text: "Current Ver.",
        filter: textFilter(),
        sort: true
    }
];

const defaultSorted = [{
    dataField: "id",
    order: "asc"
}];

const RemoteAll = ({ data, page, sizePerPage, onTableChange, totalSize }) => (
    <BootstrapTable
        remote
        striped
        hover
        condensed
        bootstrap4

        keyField="id"
        data={data}
        columns={columns}
        defaultSorted={defaultSorted}
        filter={filterFactory()}
        pagination={paginationFactory({ page, sizePerPage, totalSize })}
        onTableChange={onTableChange}
    />
);

RemoteAll.propTypes = {
    data: PropTypes.array.isRequired,
    page: PropTypes.number.isRequired,
    totalSize: PropTypes.number.isRequired,
    sizePerPage: PropTypes.number.isRequired,
    onTableChange: PropTypes.func.isRequired
};

class TableContainer extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            page: 1,
            data: [],
            totalSize: 1,
            sizePerPage: 10,
            loading: false
        };
    }

    handleTableChange = (type, { page, sizePerPage, filters, sortField, sortOrder }) => {
        console.log("handleTableChange", this.state.data);

        //const params = {
        //    page,
        //    sizePerPage,
        //    filters,
        //    sortField,
        //    sortOrder
        //};

        //axios.get("http://localhost:52632/api/database", { params })
        //    .then(response => {
        //        console.log("handleTableChange", response.data);

        //        this.setState(() => ({
        //            data: response.data,
        //            totalSize: response.data.length
        //        }));
        //    });

        this.setState(() => ({
            data: products,
            totalSize: products.length
        }))
    }

    render() {
        const { data, sizePerPage, page, totalSize } = this.state;

        return (
            <RemoteAll
                data={data}
                page={page}
                sizePerPage={sizePerPage}
                totalSize={totalSize}
                onTableChange={this.handleTableChange}
            />
        );
    }
}

export default class extends Component {
    render() {
        return (
            <TableContainer />
        );
    }
}

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:15 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
ahsan-aliicommented, Aug 18, 2022

still facing the same issue with these packages

import BootstrapTable from 'react-bootstrap-table-next'; import ToolkitProvider from 'react-bootstrap-table2-toolkit'; import paginationFactory from "react-bootstrap-table2-paginator";

I don’t know why is this issue closed, even though it exists

2reactions
joseciusshenllcommented, Jun 25, 2022

@AllenFang ToolkitProvider(default search& filter ) and Pagination not working together when I add remote for pagination.

I am also facing the same issue currently. If anybody knows please update here

Read more comments on GitHub >

github_iconTop Results From Across the Web

Cant't re-render bootstrap table when using React hooks
I am trying to render the table after adding data to it using react hooks. The data which is initially in the state...
Read more >
BootstrapTable Props · react-bootstrap-table2 - GitHub Pages
Note: when remote is enable, you are suppose to give onTableChange prop on BootstrapTable It's the only way to communicate to your remote...
Read more >
useState Hook trigger selected rows to disappear (using react ...
Coding example for the question useState Hook trigger selected rows to disappear (using react-bootstrap-table next)-Reactjs.
Read more >
Events - Bootstrap Table
Hint: if you use the jquery event handler, make sure to bind the event listener before the event is executed! onAll. jQuery Event:...
Read more >
MUI Datatables: Datatables for React using Material-UI - Morioh
If your project doesn't already use them, you need to install mui v5 and it's ... and sorting on a remote server you...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found