-
Notifications
You must be signed in to change notification settings - Fork 386
Optional searchState cleanup when unmounting a connector #892
Comments
The problem with not running the cleanup is that the search state is not longer deterministic. Do you think it would make sense to simply render nothing in your connector in a certain condition, but still render it, and thus also apply the "side effect" of applying the search? |
Hmm that's true, I hadn't considered that. My challenge is that I'm rendering a refinement list in different places depending on the device. Mobile users get the overlay which is rendered with a React portal, while other users see the refinement list elsewhere, outside the portal. If I render both at the same time, selecting a refinement will add it to the searchstate twice. I'll try to think about what can be done 🙂 |
That's a great point though, portals are a unique use case. Can you make a small reproduction of what you have (and what you'd like to change)? A template is available here |
Something like this should show the issue. If you toggle the portal, set a refinement, and toggle the portal again, the filter will be disabled "universally", so to speak, even though there's still another refinement list rendered. When it comes to how it's best to solve it, I don't know yet 🙂 Maybe the best solution would be to keep track of the count of how many times the namespace has been used by mounting components, and only remove it once all the components have been unmounted? |
this is basically the same situation i've found myself in and i'm wondering which way to turn. Part of me is wondering whether this goes beyond how instantsearch wants to be used and it's best to simply start loading the search state in to my redux store to then update the defaultRefinements (potentially?). |
@danhodkinson Yeah, in your case my namespace count idea wouldn't help, but then we're back to the prop with the issue of deterministic search state 🤔 |
indeed. Whilst i'm trying to find out if this is possible i'm going down the path of storing the search state in the redux store and syncing the main |
For reference, we discussed this with the team, but have also not found a good solution yet. We keep this of course in mind, although right now every solution seems to have the same downsides (not being deterministic). We also have a similar problem in #121. Basically some way you can go around it now is, and it's definitely not an ideal solution, to create two InstantSearch instances and synchronise their state with Please keep us updated with what things you tried! |
I think, if possible, keeping the The other part of me wonders if i'm having to go down the redux route should I be using React InstantSearch at all or should I just use the API reference. Decisions! |
It will still have a lot of advantages, specifically regarding widget design and accessibility and more complex features that require multiple requests. I'd use InstantSearch every time (and I develop both the API client and InstantSearch) |
revisiting this as i'm still having some issues. It would be great to be able to take control over what happened with clean up running. But maybe there's more to this than i'm seeing. |
or potentially even a specific I'm attempting to build my own custom connector version of this.. it sure ain't easy! |
Thanks for the inputs @danhodkinson! This kind of API has already been discussed but abandoned because it doesn't fit well with the current implementation. We still not have the perfect solution, but we will move forward for find a proper API to handle this behaviour. We will try to find a proper workaround in the coming days, we will keep you updated. |
@samouss that's great news. Out of interest, for what reason doesn't it fit well with the current implementation? |
We have a workaround that might work for your use cases. It's basically what has already been mention in the thread. You will need to keep track of the Here is a working example with the proposed workaround on CodeSandbox. The deduplication part is only used by the Let us know how it goes with your use case 🙂 class App extends Component {
state = {
isOpen: false,
brands: []
};
onSearchStateChange = searchState => {
if (searchState.refinementList) {
this.setState({
brands: Array.isArray(searchState.refinementList.brand)
? searchState.refinementList.brand.slice()
: []
});
}
};
toggle = () =>
this.setState(prevState => ({
isOpen: !prevState.isOpen
}));
render() {
const { isOpen, brands } = this.state;
return (
<InstantSearch
appId="latency"
apiKey="6be0576ff61c053d5f9a3225e2a90f76"
indexName="instant_search"
onSearchStateChange={this.onSearchStateChange}
>
<VirtualRefinementList
attributeName="brand"
defaultRefinement={brands}
/>
<SearchBox />
<button onClick={this.toggle}>Toggle</button>
{isOpen && (
<RefinementList
attributeName="brand"
defaultRefinement={brands}
/>
)}
<Hits />
</InstantSearch>
);
}
} |
Thanks @samouss, this is a solid workaround, works well with a |
I've ran into the same issue and since a year has passed since the last activity, is there any change there has been made an improvement of the RefimentList api? |
Well, have a look at this solution over here: It's written for edit: I ran into another issue when the query is also depending on other queries, for example the So .. I'm afraid I still have not found the holy grail.. |
This is still a huge difficulty with the lib. No satisfying solutions yet. |
Just voicing my concern, this is also making my life very difficult. No clean way around it. |
I just wasted 3 hours wondering why this was happening, using Modals + Portals from react-bootstrap. A warning about this wouldn't hurt. This is not expected at all. |
Hi, we now provide documentation related to showing and hiding widgets. |
Do you want to request a feature or report a bug?
Feature
Feature: What is your use case for such a feature?
I'm rendering a custom refinement list in an overlay with the
connectRefinementList
connector. I'd like the filters applied to stay on after the overlay is closed and unmounted. The connector currently runs thecleanUp
method on unmount, which removes all refinements selected. I can create my own connector but it seems like a hassle to have to copy everything but the cleanup behaviour from the existingconnectRefinementList
.Feature: What is your proposed API entry? The new option to add? What is the behavior?
I'm not sure what the best way to change the API would be here. Perhaps just a boolean prop stating whether or not to modify
searchState
on unmount?What is the version you are using? Always use the latest one before opening a bug issue.
4.4.1
The text was updated successfully, but these errors were encountered: