TanStack / table

🤖 Headless UI for building powerful tables & datagrids for TS/JS - React-Table, Vue-Table, Solid-Table, Svelte-Table
https://tanstack.com/table
MIT License
25.17k stars 3.08k forks source link

columnFilters in initialState is ignored #4763

Closed rdylina closed 1 year ago

rdylina commented 1 year ago

Describe the bug

initialState: { sorting: [{ id: "created_at", desc: false }], columnFilters:[{id: "active", value: "Active"}], },

The columnFilter is ignored. Never gets set. I can manage to work around it by setting the default state of my columnFilter state variable to the exact same array, but it's not as clean as utilizing the initialState properly.

Your minimal, reproducible example

https://codesandbox.io/p/sandbox/vigilant-jang-khs2te

Steps to reproduce

Utilize the filter example from the react-tables directly from the docs. Pick any column and set the initial state in the table declaration.

Expected behavior

On load the initial state should include the filter provided but does not.

How often does this bug happen?

None

Screenshots or Videos

No response

Platform

Windows, NextJS 13.2, React 18

react-table version

8.7.9

TypeScript version

No response

Additional context

No response

Terms & Code of Conduct

debugelton commented 1 year ago

I have the same problem. I temporarily solved this problem by setting the columnFilters state initial. ... const [columnFilters, setColumnFilters] = React.useState<ColumnFiltersState>([{id: "active", value: "Active"}]); The same can also be done with sorting .... const [sorting, setSorting] = React.useState<SortingState>([{ id: "created_at", desc: false }]); https://tanstack.com/table/v8/docs/examples/react/sorting But I'm not happy with that 🙈 😩

rdylina commented 1 year ago

Anyone have any thoughts on this one? I still cannot get the columnFilters set in the initial state to apply.

tombuntus commented 1 year ago

This is normal behaviour. Setting a property on initialState is a mechanism for initialising the table's state when you are not tracking and maintaining that state outside the table. If you set a property on state you are effectively saying "don't manage this state internally, I will manage it myself". In this case initialState will have no effect and you need to initialise your own state variable (e.g. when you call useState).