Skip to content

Commit 9d67dd0

Browse files
fixing search error by replacing 'use-query-params' with 'react-use-query-param-string'
1 parent 3c9d508 commit 9d67dd0

File tree

3 files changed

+39
-19
lines changed

3 files changed

+39
-19
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
"react-i18next": "11.12.0",
6161
"react-instantsearch-dom": "6.40.4",
6262
"react-player": "2.9.0",
63+
"react-use-query-param-string": "^2.1.5",
6364
"striptags": "3.2.0",
6465
"use-query-params": "1.2.3",
6566
"uuid": "8.3.2"

src/components/search/index.tsx

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import React, { useEffect, useState } from 'react';
2-
import { useQueryParam, encodeJson, decodeJson } from 'use-query-params';
2+
import { encodeJson } from 'use-query-params';
3+
import { useQueryParamString } from 'react-use-query-param-string';
34
import clsx from 'clsx';
45
import { InstantSearch, Index, Configure } from 'react-instantsearch-dom';
56
import { client } from '../../utils/algolia';
@@ -17,26 +18,19 @@ const environment = process.env.GATSBY_ALGOLIA_ENVIRONMENT;
1718

1819
const DEBOUNCE_TIME = 400;
1920

20-
const NullJsonParam = {
21-
encode: (obj: any | null | undefined) => {
22-
if (!obj) return undefined;
23-
return encodeJson(obj);
24-
},
25-
decode: (str: string | (string | null)[] | null | undefined) => {
26-
if (!str) return undefined;
27-
return decodeJson(str);
28-
},
29-
};
30-
3121
const Search = (): React.ReactElement | null => {
3222
const { state, dispatch } = useSearchContext();
3323
const [debouncedSetState, setDebouncedSetState] = useState<any | null>(null);
34-
35-
const [searchStateQS, setSearchStateQS] = useQueryParam('search', NullJsonParam);
24+
const [searchText, setSearchText, initialized] = useQueryParamString('search', '');
3625
const locale = currentLocale();
3726
const { t } = useTranslation();
3827
const searchScrollPosition = 'searchscrollposition';
3928

29+
const encodeSearchState = (obj: any | null | undefined) => {
30+
if (!obj) return undefined;
31+
return encodeJson(obj);
32+
}
33+
4034
useEffect(() => {
4135
const scrollpos = sessionStorage.getItem(searchScrollPosition);
4236
if (scrollpos) {
@@ -51,7 +45,8 @@ const Search = (): React.ReactElement | null => {
5145
setDebouncedSetState(
5246
setTimeout(() => {
5347
sessionStorage.setItem(searchScrollPosition, window.scrollY.toString());
54-
setSearchStateQS(updatedSearchState, 'replaceIn');
48+
const nextSearchText = encodeSearchState(state.searchState) as string;
49+
setSearchText(nextSearchText);
5550
}, DEBOUNCE_TIME),
5651
);
5752
dispatch({ type: 'set-search-state', payload: updatedSearchState });
@@ -64,12 +59,13 @@ const Search = (): React.ReactElement | null => {
6459
};
6560

6661
useEffect(() => {
67-
setSearchStateQS(state.searchActive ? state.searchState : null, 'replaceIn');
62+
const nextSearchText = state.searchActive ? encodeSearchState(state.searchState) as string : '';
63+
setSearchText(nextSearchText);
6864
}, [state.searchActive]);
6965

7066
useEffect(() => {
71-
if (searchStateQS) {
72-
dispatch({ type: 'set-search-state', payload: searchStateQS });
67+
if (searchText && initialized) {
68+
dispatch({ type: 'set-search-state', payload: { query: searchText } });
7369
dispatch({ type: 'set-search-active', payload: true });
7470
}
7571
document.addEventListener('keydown', handleEsc);

yarn.lock

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5003,7 +5003,7 @@ decamelize@^1.2.0:
50035003
resolved "https://registry.npmjs.org/decamelize/-/decamelize-1.2.0.tgz"
50045004
integrity sha512-z2S+W9X73hAUUki+N+9Za2lBlun89zigOyGrsax+KUQ6wKW4ZoWpEYBkGhQjwAjjDCkWxhY0VKEhk8wzY7F5cA==
50055005

5006-
decode-uri-component@^0.2.0:
5006+
decode-uri-component@^0.2.0, decode-uri-component@^0.2.2:
50075007
version "0.2.2"
50085008
resolved "https://registry.npmjs.org/decode-uri-component/-/decode-uri-component-0.2.2.tgz"
50095009
integrity sha512-FqUYQ+8o158GyGTrMFJms9qh3CqTKvAqgqsTnkLI8sKu0028orqBhxNMFkFen0zGyg6epACD32pjVk58ngIErQ==
@@ -10929,6 +10929,16 @@ query-string@^6.14.1:
1092910929
split-on-first "^1.0.0"
1093010930
strict-uri-encode "^2.0.0"
1093110931

10932+
query-string@^7.1.1:
10933+
version "7.1.3"
10934+
resolved "https://registry.yarnpkg.com/query-string/-/query-string-7.1.3.tgz#a1cf90e994abb113a325804a972d98276fe02328"
10935+
integrity sha512-hh2WYhq4fi8+b+/2Kg9CEge4fDPvHS534aOOvOZeQ3+Vf2mCFsaFBYj0i+iXcAq6I9Vzp5fjMFBlONvayDC1qg==
10936+
dependencies:
10937+
decode-uri-component "^0.2.2"
10938+
filter-obj "^1.1.0"
10939+
split-on-first "^1.0.0"
10940+
strict-uri-encode "^2.0.0"
10941+
1093210942
queue-microtask@^1.2.2:
1093310943
version "1.2.3"
1093410944
resolved "https://registry.npmjs.org/queue-microtask/-/queue-microtask-1.2.3.tgz"
@@ -11142,6 +11152,19 @@ react-side-effect@^2.1.0:
1114211152
resolved "https://registry.npmjs.org/react-side-effect/-/react-side-effect-2.1.2.tgz"
1114311153
integrity sha512-PVjOcvVOyIILrYoyGEpDN3vmYNLdy1CajSFNt4TDsVQC5KpTijDvWVoR+/7Rz2xT978D8/ZtFceXxzsPwZEDvw==
1114411154

11155+
react-sub-unsub@^2.1.6:
11156+
version "2.2.7"
11157+
resolved "https://registry.yarnpkg.com/react-sub-unsub/-/react-sub-unsub-2.2.7.tgz#d755ea40ab61ff64bc8c73a9013efa910472629d"
11158+
integrity sha512-b2o0mIW8G4Yb3aaKxFB9iiCCHxCDGmogy+493oQpEJHjBy/hl6uf+6RhAinqKWRwi1fvO6mGIMVGsf2XYLL38g==
11159+
11160+
react-use-query-param-string@^2.1.5:
11161+
version "2.1.5"
11162+
resolved "https://registry.yarnpkg.com/react-use-query-param-string/-/react-use-query-param-string-2.1.5.tgz#d0b43d87e43fd64067b23009f78be85351d6bd09"
11163+
integrity sha512-h9UbTwDrVUlZ2m1iBJOWrBN/dWJ48dT0YKs45xrouxr73b7aBDQ8QRT6XhEpY6I9buy5YR+O5k/eHh5Aewte1g==
11164+
dependencies:
11165+
query-string "^7.1.1"
11166+
react-sub-unsub "^2.1.6"
11167+
1114511168
1114611169
version "17.0.1"
1114711170
resolved "https://registry.npmjs.org/react/-/react-17.0.1.tgz"

0 commit comments

Comments
 (0)