setSortby not defined
I am updating the state of sortBy as below with setSortBy, but I notice error setSortBy not defined on both javascript and typescript environment. Any leads is appreciated.
Codesandbox: https://codesandbox.io/s/restless-firefly-kmoeq?from-embed=&file=/src/App.js
From React-table documentation: setSortBy: Function(sortBy: Array<Object<id: columnId, desc: Bool = true>>) => void
This function can be use to programmatically set the sortBy for the table instance
Is it possible to update the state of the sortBy using any other approach.
@at-webdev it's part of the table instance so, you should:
...
rows,
prepareRow,
setSortBy, <--- add here
state: { sortBy }
} = useTable(
Also, this seems to work for me, no need to wrap in an inner function:
setSortBy([{ id: 'firstName', desc: false }])
Thanks @david-potgieter, but I am not sure why itsnt working on previous sandbox link.
For those wanting to implement setSortby on previous versions useControlledState would fix this.
const { rows, ...., prepareRow, state } = useTable({
columns: columns,
data: data,
initialState: {
sortBy: [{ id: "age", desc: false }]
},
useControlledState: state => {
return React.useMemo(
() => ({
...state,
sortBy: [{ id: "firstName", desc: false }] // similar to setSortBy you can set here
}),
[state]
);
}
});
Also seeing the same issue - setSortBy is undefined on the table instance
This issue is either:
- Outdated
- Stale
- Fixed in v8
- Unable to be fixed in v7 without breaking changes and is slated for v8
If you feel this is incorrect, please reopen a new issue with a codesandbox reproduction or more info regarding the issue.