Custom function passed into setFilters doesn't get called
See original GitHub issueWhat you were expecting: setFilters callback to get called when user types into SearchInput component.
What happened instead: The custom setFilters function parsed into Filter component doesn’t get called. Instead, the default setFilters get called with the value in the searchInput instead.
Steps to reproduce:
- in PostFilter component (from codesandbox), pass in a custom function to the setFilter prop with a console log. Please see below in related code area or in codesandbox for reference.
- Type in the search box, expect console log to show
- console log doesn’t show. Instead the setFilter was default to the function below
function (filters, displayedFilters) { return debouncedSetFilters(filters, displayedFilters); }
which is not the input function into props of Filter component. Perhaps the above function came from here? useListParams
Related code:
CodeSandbox with sample issue link https://codesandbox.io/s/restless-https-x3d05?file=/src/posts/PostList.js:1185-1226 forked from https://codesandbox.io/s/github/marmelab/react-admin/tree/master/examples/simple
As shown in the CodeSandbox above I would expect the below code to show a console.log(“inside customSetFilters”) on the console, but the function customSetFilters never get called so the log never appears.
const PostFilter = (props) => {
const { setFilters, ...rest } = props;
const customSetFilters = (filter) => {
console.log("inside customSetFilters");
return setFilters(filter);
};
return (
<Filter setFilters={customSetFilters} {...rest}>
<SearchInput source="q" alwaysOn />
<TextInput
source="title"
defaultValue="Qui tempore rerum et voluptates"
/>
<QuickFilter
label="resources.posts.fields.commentable"
source="commentable"
defaultValue
/>
</Filter>
);
};
Other information:
My suspicion here is that for some reason filterForm doesn’t call the correct setFilters function here https://github.com/marmelab/react-admin/blob/dcb2a2e71f595dff8440d64001d033aa2d98afa1/packages/ra-ui-materialui/src/list/filter/FilterForm.tsx#L193
Environment
- React-admin version:3.8.4
- React version: 16.13.1
- Browser: Chrome
- Stack trace (in case of a JS error):
Issue Analytics
- State:
- Created 3 years ago
- Reactions:2
- Comments:7 (4 by maintainers)
Top GitHub Comments
Now I understand the problem.
useListContext
normally returns thesetFilters
callback from the context. However, to avoid breaking compatibility when introducing the ListContext, we’ve passed theprops
touseListContext
. That way, If you useFilter
outside of aListContext
, it will grab thesetFilters
callback from the props.But in your case, you are inside a
ListContext
(it’s created byList
). The fallback toprops
doesn’t work in this case.The fix on your side is to pass your custom
setFilters
not via props, by via a custom ListContext. Something like:As a side note, overriding the
setFilters
callback wasn’t a documented feature, and as such isn’t supported. So I don’t think this is really a bug in react-admin and I’m not sure we should fix it.@fzaninotto Thank you Francois. Your explanation made sense. Tried it on my end with the method you suggested and things work well now. 🙆♂️