Initial Filtered State in Manual Mode?
What version of React-Table are you using?
6.6.0
What bug are you experiencing, or what feature are you proposing?
Is there a way to have the table initial render with a set of filtered key/values when in manual mode? Calling setState on the component with filtered keys seems to cause some issues (maybe, perhaps because it then it in turn calls the onFetchData callback and maybe something isn't copacetic there). Wondering if it's possible to set the filtered state on init but not have it call fetchData until after the user interacts with the component.
Example: I have a query string parameter in the URL bar -- which in turn maps to a set of filters which I pass as props down to the table. Once the user interacts with the table's filtering, it becomes the source of truth or it emits an action to keep it synced in a redux store.
Please provide a codesandbox.io example of the problem you are experiencing. It should be possible to set the filtered parameter but maybe your implementation is causing an infinite loop.
@gary-menzel I can try and put together a code pen -- but I think my only issue is that I can't disable the onFetchData from firing until a user physically interacts with the table. If this was an option my issues would be resolved 100%. Do you know if there is a way currently with the code for this to happen, or would this be a feature request?
Once my parent component calls setState on the ReactTable component -- since setState is asynchronous the componentDidMount inside of ReactTable will fire fireFetchData before the filters have had a chance to be set on the component. I could potentially hack around this by just manually setting the filtered state values without calling setState but that seems like a bad work around.
An ideal solution would be able to have a flag where we could disable fireFetchData until one of the ReactTable's inputs onChange event fires.
Believe i have the exact same issue, setting 'filtered' with a default array causes onFetchData to go into an infinite loop - https://pastebin.com/Q8g77ybJ
@learningassistant I kinda had to hack around it bit by when I call "setState" with my initial set of filters, on the onFetchData callback, I had to do some figuring to make sure what the table is then trying to fetch isn't the same as what I set initially. It's a little more complex than a lodash _.isEqual comparison, but it gets the job done. Hacky - yes.
componentWillReceiveProps(nextProps) {
const table = this.table;
const filtered = [];
_.each(nextProps.filters || {}, (v, k) => {
if (!['pageNumber', 'pageSize', 'sortAttr', 'sortDir', 'tz'].includes(k)) {
filtered.push({ id: k, value: v });
if (!this.filterKeys.includes(k) && !_.isUndefined(v)) {
this.filterKeys.push(k);
}
}
});
table.state.filtered = filtered;
table.setState({ filtered });
}
fetchData(state) {
const { payload = {}, query = {}, queryState = {} } = this.props;
const { filters = {} } = this.state;
const { busy = false } = queryState;
/**
* Don't fetch if we are already in the process of getting data
*/
if (busy === true) {
return;
}
const newFilters = {
...filters,
};
_.each(this.filterKeys, (k) => {
newFilters[k] = undefined; // necessary because otherwise we can't "unset" a filter that becomes controlled by the table
});
const { pageSize, page, sorted } = state; // table state
const pageNumber = page + 1;
newFilters.pageNumber = pageNumber;
newFilters.pageSize = pageSize;
let sortAttr;
let sortDir;
if (sorted.length) {
const desc = sorted[0].desc;
sortDir = 'asc';
if (desc) sortDir = 'desc';
sortAttr = sorted[0].id;
newFilters.sortDir = sortDir;
newFilters.sortAttr = sortAttr;
}
let doFilters = [];
if (this.table) {
doFilters = doFilters.concat(this.table.state.filtered);
}
doFilters.concat(state.filtered);
_.each(doFilters, (f) => {
const value = f.value;
if (value === undefined) return;
const id = f.id;
newFilters[id] = value;
if (!this.filterKeys.includes(id)) {
this.filterKeys.push(id);
}
});
newFilters.tz = 'UTC';
/**
* Don't fetch again if we are going to be sending the same
* filter parameters
*/
if (_.isEqual(this.lastQuery, newFilters)) {
return;
}
this.lastQuery = {
...newFilters,
};
this.props.fetch({ payload, query: newFilters });
}
Closing due to issue age. If this issue was a question, please ask it again on https://spectrum.chat/react-table. If you think this issue needs to be reopened or if it should be turned into a pull-request, please check the latest version of React-Table for the issue or feature once more and reopen or create a PR if appropriate. Thanks!
You keep your filter stored in state and pass that on each re-render as follows
take a initial state filtered: [],
pass this as a props to table filtered={this.state.filtered}
add onChange event to table onFilteredChange = {filtered => {this.setState({ filtered });}}
So now once you filter anything it will be set to filtered state and then passed to table
Believe i have the exact same issue, setting 'filtered' with a default array causes onFetchData to go into an infinite loop - https://pastebin.com/Q8g77ybJ
Having this same exact issue, table uses manual mode, onFetchData continues to fire in infinite loop when I try to set the filtered prop of the table...any advice is appreciated! Thank you!