set defaultSort programmatically
Describe the bug I was set config and data from state programmatically but result doesn't sort
To Reproduce when data and config was bind, result doesn't sorted as config
Expected behavior result should be sorted
export default function TestReport() {
const [data, setData] = useState([]);
const [config, setConfig] = useState([]);
useEffect(() => {
setData(getData());
setConfig(getConfig());
}, []);
const getData = () => {
return [
{ name: 'Mehmet', year: 1987 },
{ name: 'Zerya Betül', year: 2017 }
];
};
const getConfig = () => {
return [
{ title: 'Name', field: 'name' },
{ title: 'Year', field: 'year', defaultSort: 'desc' }
];
};
return <MaterialTable columns={config} data={data} />;
}
Screenshots

Expected

material-table v1.57.2
have you set sort:true ?
have you set sort:true ?
did you mean sorting:true ? I believe default value of sorting is true, both options and columns
I just battled with this today too. Think i figured it out.
The first paint (render of the component) will have empty values, because the useState values are defaulted to []. This will render the Table, with its columns == []. This means that he columns do NOT have defaults. ie the defaultSort is null for both. When you run the effect, it sets the VALUES of the columns, but the defaults have no effect. It seems that the defaults are only use in the first paint. To refresh my values and the sort values. I make sure that the table is fully re-rendered. I did this in my app by fully removing the table when loading, then re placing it once data is loaded.
It seems like a bug to me

https://codesandbox.io/s/trusting-hertz-w1ufb?file=/src/App.js:691-778
make sure you hit the refresh button on the preview there to get the initial load values
yes, It seem like a bug, defaultSort are inside "columns", Columns are changeable by useEffect. so, defaultSort should be changeable as well.
You can use the following workaround for now, if you would like..
import React, { useState, useEffect } from "react";
import { render } from "react-dom";
import MaterialTable from "material-table";
import tableIcons from "./TableIcons.js";
const App = () => {
const [data, setData] = useState([]);
const [config, setConfig] = useState([]);
useEffect(() => {
const sortedData = sortByProp(getData(), "year", "desc");
setData(sortedData);
setConfig(getConfig());
}, []);
const sortByProp = (arr = [], property, ascOrDesc) => {
if (
!arr.length ||
!property ||
!["asc", "desc"].includes(ascOrDesc.toLowerCase())
) return;
let sortingAlgo = (x, y) => (x[property] < y[property] ? 1 : -1);
if (ascOrDesc === "asc") {
sortingAlgo = (x, y) => (x[property] > y[property] ? 1 : -1);
}
return arr.sort(sortingAlgo);
};
const getData = () => {
return [
{ name: "Mehmet", year: 1987 },
{ name: "Zerya Betül", year: 2017 }
];
};
const getConfig = () => {
return [
{ title: "Name", field: "name" },
{ title: "Year", field: "year", defaultSort: "desc" }
];
};
return <MaterialTable columns={config} data={data} icons={tableIcons} />;
};
render(<App />, document.querySelector("#root"));
You can use the following workaround for now, if you would like..
import React, { useState, useEffect } from "react"; import { render } from "react-dom"; import MaterialTable from "material-table"; import tableIcons from "./TableIcons.js"; const App = () => { const [data, setData] = useState([]); const [config, setConfig] = useState([]); useEffect(() => { const sortedData = sortByProp(getData(), "year", "desc"); setData(sortedData); setConfig(getConfig()); }, []); const sortByProp = (arr = [], property, ascOrDesc) => { if ( !arr.length || !property || !["asc", "desc"].includes(ascOrDesc.toLowerCase()) ) return; let sortingAlgo = (x, y) => (x[property] < y[property] ? 1 : -1); if (ascOrDesc === "asc") { sortingAlgo = (x, y) => (x[property] > y[property] ? 1 : -1); } return arr.sort(sortingAlgo); }; const getData = () => { return [ { name: "Mehmet", year: 1987 }, { name: "Zerya Betül", year: 2017 } ]; }; const getConfig = () => { return [ { title: "Name", field: "name" }, { title: "Year", field: "year", defaultSort: "desc" } ]; }; return <MaterialTable columns={config} data={data} icons={tableIcons} />; }; render(<App />, document.querySelector("#root"));
this will work only first render, I'm asking for programmatically sorting.
as columns can be changed by state, so columns.defaultSort should be changed as well
@hongjs that should be fixed by the latest release. Can you try it out and reopen the issue if needed. Your initial code displayed in the initial description.
@Domino987 Thank you so much for the help