Skip to content
This repository was archived by the owner on Dec 30, 2022. It is now read-only.

Commit f85d679

Browse files
fix(hooks): apply initial search parameters in useConnector (#3276)
* wip: apply initial search parameters in hooks * alternative location * shorter * this fixes the following case too * Apply suggestions from code review Co-authored-by: François Chalifour <[email protected]> Co-authored-by: François Chalifour <[email protected]>
1 parent c481232 commit f85d679

File tree

3 files changed

+42
-2
lines changed

3 files changed

+42
-2
lines changed

packages/react-instantsearch-hooks/src/connectors/__tests__/useHitsPerPage.test.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ describe('useHitsPerPage', () => {
2727
items: [
2828
{
2929
default: true,
30-
isRefined: false,
30+
isRefined: true,
3131
label: '4 hits per page',
3232
value: 4,
3333
},

packages/react-instantsearch-hooks/src/connectors/__tests__/useSearchBox.test.tsx

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,35 @@ describe('useSearchBox', () => {
2828
refine: expect.any(Function),
2929
});
3030
});
31+
32+
test('returns the connector render state with initialUiState', async () => {
33+
const wrapper = createInstantSearchTestWrapper({
34+
initialUiState: {
35+
indexName: {
36+
query: 'testio',
37+
},
38+
},
39+
});
40+
const { result, waitForNextUpdate } = renderHook(() => useSearchBox(), {
41+
wrapper,
42+
});
43+
44+
// Initial render state from manual `getWidgetRenderState`
45+
expect(result.current).toEqual({
46+
query: 'testio',
47+
isSearchStalled: false,
48+
clear: expect.any(Function),
49+
refine: expect.any(Function),
50+
});
51+
52+
await waitForNextUpdate();
53+
54+
// InstantSearch.js state from the `render` lifecycle step
55+
expect(result.current).toEqual({
56+
query: 'testio',
57+
isSearchStalled: false,
58+
clear: expect.any(Function),
59+
refine: expect.any(Function),
60+
});
61+
});
3162
});

packages/react-instantsearch-hooks/src/hooks/useConnector.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,16 @@ export function useConnector<
7575
// The helper exists because we've started InstantSearch.
7676
const helper = parentIndex.getHelper()!;
7777
const results =
78-
parentIndex.getResults() || createSearchResults(helper.state);
78+
// On SSR, we get the results injected on the Index.
79+
parentIndex.getResults() ||
80+
// On the browser, we create fallback results based on the widget's
81+
// `getWidgetSearchParameters()` method to inject the initial UI state,
82+
// or fall back to the helper state.
83+
createSearchResults(
84+
widget.getWidgetSearchParameters?.(helper.state, {
85+
uiState: parentIndex.getWidgetUiState({})[parentIndex.getIndexId()],
86+
}) || helper.state
87+
);
7988
const scopedResults = parentIndex
8089
.getScopedResults()
8190
.map((scopedResult) => {

0 commit comments

Comments
 (0)