BootstrapTable doesn't re-render after setState when remote/onTableChange is used
See original GitHub issueI’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:
- Created 5 years ago
- Comments:15 (3 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

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
I am also facing the same issue currently. If anybody knows please update here