react-bootstrap-table icon indicating copy to clipboard operation
react-bootstrap-table copied to clipboard

dynamic columns + remote filters + setting filter defaultValue on re-render = infinite loop

Open zhoplocal opened this issue 9 years ago • 6 comments

Huge fan of this project. Thanks for maintaining it!

I've got a problem using filters on dynamic columns. Here is what happens. I've got a list of users that I'm fetching from the server and storing it in redux store.

The server returns data and columns that should be rendered dynamically. Each of the columns has filters set dynamically by calling a function which depending on the field type returns the appropriate filter with defaultValue initially set to empty string or an empty object depending on the type of filter.

On the first render, everything works just fine. Say I've two columns name and email that were dynamically rendered and filter set to TextFilter.

Now when I enter the search term in the filter input the OnFilterChange triggers and it dispatches the redux action to fetch the data matching the search term. On getting that data from the server the table re-renders and I lose the search term that was entered previously so I end up setting the defaultValue on the filter.

But setting the defaultValue on the filter triggers the OnFilterChange again and it goes into an infinite loop.

I don't know if I did a good job explaining it. Please let me know if I'm missing something or doing something wrong.

BTW on my other project where I don't use redux this setup works perfectly fine.

zhoplocal avatar May 11 '17 08:05 zhoplocal

Might not have anything to do with the dynamic columns. Same thing happens with static columns as well.

zhoplocal avatar May 11 '17 08:05 zhoplocal

Nevermind it was an issue on my end. The table was wrapped by a content placeholder component which was remounting the table component on every request.

zhoplocal avatar May 11 '17 16:05 zhoplocal

@zhoplocal, sorry for lately reply, thanks your clear explain, I think I've two solution to fix it, but not sure it is work:

  1. I can try to keep the filters state when it re-render, so if user type something in filter and trigger your redux action then fetch new data and re-render table, the text on filter will be keep there and not trigger OnFilterChange

  2. Support developer can configure the text on filter but doesn't trigger the filtering, only defaultValue will trigger it. So after first filter, you can add the text on filter when rendering table.

Let me know if you have any concern, thanks a lots

AllenFang avatar May 12 '17 13:05 AllenFang

I'm running into this very issue and this bug wasn't there before 3.4.5. In fact, I managed to track it down to this commit: fix #1445 and I think the cause of it is the code in componentDidMount() which is firing a filter change with the "defaultValue" while in fact it isn't the default value which was set on the state but const defaultValue = this.refs.inputText.value;.

This in turn triggers the onFilterChange with a blank filterValue while afterwards this component receives it's new props to set as the defaultValue which causes a new back-end request and in turn generates a new componentDidMount() etc...

We're also wrapping the TableColumnHeader in a self made component.

bartvanremortele avatar Aug 02 '17 15:08 bartvanremortele

@bartvanremortele sorry for lately reply, do you have any example for me to reproduce it? I wanna check this use case, but please keep example simple and minimal, thanks

AllenFang avatar Aug 07 '17 13:08 AllenFang

@zhoplocal, sorry for lately reply, thanks your clear explain, I think I've two solution to fix it, but not sure it is work:

  1. I can try to keep the filters state when it re-render, so if user type something in filter and trigger your redux action then fetch new data and re-render table, the text on filter will be keep there and not trigger OnFilterChange
  2. Support developer can configure the text on filter but doesn't trigger the filtering, only defaultValue will trigger it. So after first filter, you can add the text on filter when rendering table.

Let me know if you have any concern, thanks a lots

I am doing something similar but how do we preserve the filter options for select filter? I am showing dynamic options but as soon as i click on one the data changes so the options changes too.

dashName avatar May 27 '20 21:05 dashName