[react-instantsearch-hooks] RefinementList search state cleared on un-mount even when using useRefinementList to create a virtual widget
See original GitHub issue🐛 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 the useRefinementsList
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:
- Go to https://codesandbox.io/s/snowy-field-22wyku?file=/src/App.js
- Click on the “Show DIalog” button to open the modal
- Apply a refinement (notice that the hits have updated)
- Close the modal by clicking outside the modal body
- The refinement has reset and all results show (refinement state has been cleared)
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
- React InstantSearch Hooks version: 6.29.0
- React version: 18.0.0
- Browser: Chrome 103.0.5060.53
- OS: macOS
Thanks
Issue Analytics
- State:
- Created a year ago
- Reactions:3
- Comments:9 (2 by maintainers)
Top Results From Across the Web
[react-instantsearch-hooks] RefinementList search state ...
[react-instantsearch-hooks] RefinementList search state cleared on un-mount even when using useRefinementList to create a virtual widget #5240.
Read more >Show and hide React InstantSearch Hooks widgets - Algolia
Keep the widget mounted but hidden; Persist the state on unmount using a virtual widget; Use the Hook in a parent component. Keep...
Read more >Algolia - Store RefinementList value when close Modal
You always need to have the RefinementList mounted in the application so that the state is persisted in React InstantSearch's internal state.
Read more >react-instantsearch-dom | Yarn - Package Manager
owner algolia800.7kMIT6.38.1DT · vulns 0 vulnerabilities. ⚡ Lightning-fast search for React DOM, by Algolia. algolia, components, fast, instantsearch ...
Read more >Algolia - Making a negative filter widget for InstantSearch
6 open source libraries. 11 API clients. 1 hour to build. Start now, it's free for 15 days on www.algolia.comThe topic of this...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
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) https://chakra-ui.com/docs/components/drawer
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
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/