Skip to content

Commit

Permalink
dispose widgets in both modes
Browse files Browse the repository at this point in the history
  • Loading branch information
dhayab committed Sep 13, 2023
1 parent 4bbd60a commit de926c3
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 24 deletions.
1 change: 1 addition & 0 deletions examples/react/getting-started/src/App.css
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ em {

.search-panel {
display: flex;
gap: 1rem;
}

.search-panel__filters {
Expand Down
35 changes: 11 additions & 24 deletions examples/react/getting-started/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import {
Pagination,
RefinementList,
SearchBox,
useRefinementList,
DynamicWidgets,
CurrentRefinements,
} from 'react-instantsearch';

import { Panel } from './Panel';
Expand Down Expand Up @@ -58,21 +59,22 @@ export function App() {
Toggle filters
</button>
{showFilters && (
<Panel header="brand">
<RefinementList attribute="brand" />
</Panel>
<DynamicWidgets fallbackComponent={FallbackComponent} />
)}
</div>

<div className="search-panel__results">
<SearchBox placeholder="" className="searchbox" />
<CustomBrandRefinementList />
<CurrentRefinements />
<Hits hitComponent={HitComponent} />

<div className="pagination">
<Pagination />
</div>
</div>
<div className="search-panel__filters">
<DynamicWidgets fallbackComponent={FallbackComponent} />
</div>
</div>
</InstantSearch>
</div>
Expand All @@ -97,25 +99,10 @@ function HitComponent({ hit }: HitProps) {
);
}

function CustomBrandRefinementList() {
const { items } = useRefinementList({ attribute: 'brand' });
const selectedItems = items
.filter((item) => item.isRefined)
.map((item) => item.label);

function FallbackComponent({ attribute }: any) {
return (
<div
style={{
border: '1px solid gray',
padding: '1rem',
marginBottom: '1rem',
}}
>
<pre>{`useRefinementList({ attribute: 'brand' })`}</pre>
<small>
<strong>Selected brands: </strong>{' '}
{selectedItems.length ? selectedItems.join(', ') : 'none'}
</small>
</div>
<Panel header={attribute}>
<RefinementList attribute={attribute} />
</Panel>
);
}
9 changes: 9 additions & 0 deletions packages/instantsearch.js/src/widgets/index/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,15 @@ const index = (widgetParams: IndexWidgetParams): IndexWidget => {
})
);
} else {
// Dispose all widgets without keeping the mutated state
widgets.forEach((widget) => {
widget.dispose!({
helper: helper!,
state: helper!.state,
parent: this,
});
});

// Update helper state with previous localUiState
const initialSearchParameters =
new algoliasearchHelper.SearchParameters({
Expand Down

0 comments on commit de926c3

Please sign in to comment.