-
Notifications
You must be signed in to change notification settings - Fork 531
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[react-instantsearch-hooks] RefinementList search state cleared on un-mount even when using useRefinementList to create a virtual widget #5240
Comments
Unfortunately this isn't yet possible. There's two possible workarounds:
function RefinementList() {
useRefinementList()
return <Dialog />
}
We don't yet have a mechanism to prevent a widget from cleaning up when it unmounts, even when there's another widget of the same type in the tree, as it's impossible to detect both are intended to be synced |
Here's how to use the internal ui component to avoid recreating the whole rendering and still put the dialog inside the refinement list conceptually: https://codesandbox.io/s/autumn-leftpad-3j3nmp?file=/src/App.js |
We are running into the same issue. We use a drawer like structure on mobile, while keeping the desktop UI components mounted. The drawer is a component from @chakra-ui/react, of which we can't control the mounting state (it gets unmounted on close) The drawer opens if you click a filter button, the user can apply refinements within the drawer but once the drawer closes, all filters cleared (as the drawer simply renders all the filters within). It feels like unexpected behaviour, as I would expect the lib to keep track of the mounted widgets We'll look intro restructuring our component tree for now. [edit] please note that both the desktop and mobile UI use hooks The documentation actually seems to suggest that the @jamespeilow is pointing out should actually work: https://www.algolia.com/doc/guides/building-search-ui/widgets/customize-an-existing-widget/react-hooks/#building-a-virtual-widget-with-hooks |
had another look the connect example from here which uses a modal https://www.algolia.com/doc/guides/building-search-ui/going-further/native/react/#create-a-modal it can also be done in hooks by using const VirtualUiState = ({ searchState }) => {
const { setUiState } = useInstantSearch();
useEffect(() => {
setUiState(searchState);
}, [setUiState, searchState]);
return null;
}; const VirtualRefinementList = ({ attribute, searchState }) => {
useRefinementList({ attribute });
return null
}; ...
const [searchState, setSearchState] = useState({})
const handleStateChange = ({ uiState, setUiState }) => {
const nextState = {...uiState} // or custom merge
setSearchState(nextState);
setUiState(nextState);
}
... <InstantSearch searchClient={searchClient} indexName="instant_search" >
<VirtualUiState searchState={searchState} />
<VirtualRefinementList searchState={searchState} attribute={attribute} />
{filterVisible && (
<InstantSearch
searchClient={searchClient}
indexName="instant_search"
onStateChange={handleStateChange}
initialUiState={searchState}
>
<Panel header={attribute}>
<RefinementList attribute={attribute} />
</Panel>
</InstantSearch>
)}
</InstantSearch> working version here https://codesandbox.io/s/snowy-leftpad-cbln77?file=/src/App.tsx:954-1145 @Haroenv the hooks documentation should be updated with this example |
We've fixed this issue in our app with this wrapper component for a widget to prevent the unloading state behaviour const WidgetUiStateProvider = ({
indexName,
children,
searchClient,
syncWithRootUiState = true,
}) => {
const {
uiState: rootUiState,
setUiState: setRootUiState,
} = useInstantSearch();
const handleStateChange = ({ uiState, setUiState }) => {
if (syncWithRootUiState) {
setUiState(uiState);
setRootUiState(uiState);
}
};
return (
<InstantSearch
indexName={indexName}
searchClient={searchClient}
onStateChange={handleStateChange}
initialUiState={rootUiState}
>
{children}
</InstantSearch>
);
}; it can be used like so <InstantSearch searchClient={searchClient} indexName="instant_search">
<Configure hitsPerPage={8} />
<VirtualRefinementList attribute={attribute} />
...
{filterVisible && (
<WidgetUiStateProvider
searchClient={searchClient}
indexName="instant_search"
>
<Panel header={attribute}>
<RefinementList attribute={attribute} />
</Panel>
</WidgetUiStateProvider>
)}
...
</InstantSearch> https://codesandbox.io/s/solitary-mountain-d47veh?file=/src/App.tsx:2708-3066 |
This is the fiddliest thing I've dealt with today. Thankyou @fatlinesofcode for the hint, would not have figured this out without your help. A note on my struggles implementing it in my code:
|
To anyone who doesn't want the added complexity of maintaining search state and virtual widgets just to get a drawer working w/ React on mobile, Material UI drawer has the ability to hide without unmounting, so you can use regular refinement lists without any of the workarounds above. Funny enough, I love Chakra UI and my entire project uses it except for just this single drawer on mobile breakpoints 😝. Material UI drawer: https://mui.com/material-ui/react-drawer/
|
I've ran into this same issue but a little different. First, I am only hiding the refinements. If a refinement has a transformItems function set, it loses its state on hide, none of my other refinements lose state. I have not tried the state provider example from @fatlinesofcode yet but will see if that works this weekend. |
So I just had time to test this and the state handler @fatlinesofcode proposed does not solve the issue of RefinementList with transformItems function set losing it's state when hiding. <RefinementList
transformItems={
(items) => items.map((item) => ({
...item,
label: RefinementTypeMap.get(item.label) || item.label
}))
}
attribute="type" /> As soon as the refinements are hidden, it loses the selection however all my other refinements without transformItems do not. |
In the past, we were able to avoid clean on unMount by deleting the Is there any way, in
EDITNevermind, we were wrong and successfuly reach this objective, overriding
|
We have a similar issue with react-native an a Modal with refinements. So i made a similar solution like @AntoineDuComptoirDesPharmacies for useRefinementList
|
This should actually be fixed with the |
🐛 Bug description
When placing a
<RefinementList>
component inside a modal component that mounts and un-mounts its content, the current refinements are cleared even when using theuseRefinementsList
hook to create a virtual refinement list widget.🔍 Bug reproduction
Minimal example set up on Codesandbox, using the Algolia docs example as a reference.
Steps to reproduce the behavior:
Live reproduction:
https://codesandbox.io/s/snowy-field-22wyku?file=/src/App.js
💭 Expected behavior
Current refinement search state should be preserved when the
<RefinementList>
component inside the modal component is unmounted on close.Environment
Thanks
The text was updated successfully, but these errors were encountered: