Does react-table fire an event I can attach a callback to when applying a filter?
What version of React-Table are you using?
[email protected] [email protected]
What bug are you experiencing, or what feature are you proposing?
This isn't a bug, more of a support question. Should I post simple usage questions on stackoverflow with a react-table tag?
I have export-to-xls and export-to-csv methods in place for a working module that contains a react-table. Everything works just fine but I need to check the size of the sorted table (columns x state.sortedData.length) to make adjustments to the export options.
Is there a prop I can attach a callback to every time a new filter is applied? I can get the data length easily (as above). I would prefer not to make the calculation while my custom filters are being applied as it seems like a race condition danger.
Thanks in advance!
John
Use onFilteredChange, capture the single prop (filtered) and set that in your set (this.setState) so you can then pass it in as a prop to your ReactTable instance (<ReactTable... filtered={this.state.filtered}.... />) BUT.... in your setState, add a function as the 2nd parameter and do your calculations there. It will only be called after the state has been applied.
@gary-menzel thanks for the suggestion!
This is a redux application so my state manipulation is a little different.
Given this ReactTable instantiation:
<ReactTable
data={ data }
columns={ columns }
defaultPageSize={ 10 }
filterable
filtered={this.props.reportFilter}
sortable={true}
onFilteredChange={filtered => {
this.handleAppliedFilter.bind(this, filtered);
}}
<...>
/>
and this handler function:
handleAppliedFilter = (filtered) => {
console.log("filtered: ");
console.log(filtered);
this.props.emitSetAppliedFilter(filtered);
}
handleAppliedFilter is never called. Indeed, watching the activity in Chrome's React Tools neither filtered nor onFilteredChange register any activity when filtering, though the filter(s) work fine.
Am I missing something?
Thanks again,
JB
Drop a console.log into onFilteredChange just before you call the Redux handler and see if that outputs. The basic logic around the ReactTable instance looks right but there are places outside of the code you have show that still may not be correct.
This is best mocked up in codesanbox.io and then run by more people in the #react-table Slack channel (badge at the top of the README).
@gary-menzel Following your lead above, I was able to capture filtered and feed it back into the ReactTable component with ease. My handler was not firing due to a this/bind context issue. The event handler fires just fine now.
Unfortunately, there is no way that I see in an external (to ReactTable) function to access the table's state.sortedData array so I think I'm out of luck in that direction anyway.
However, I am using the "functional rendering" ability to render a set of control buttons under the table. The simple addition of a render event handler outside the return does the trick and allows me to assess the size (length) of the filtered set:
<ReactTable
data={ data }
columns={ dataColumns }
defaultPageSize={ 10 }
{ ... }
>
{(state, makeTable, instance) => {
this.handleAppliedFilter(state.sortedData.length);
return (
<div>
{makeTable()}
{ ... }
</div>
)
}}
</ReactTable>
Thanks for your help!
JB