-
I'm working with React InstantSearch Hooks, and I'm trying to configure custom routing to work in a similar fashion as the previous version of the site. The site has page number as a 1-based So for example, if I access the page with Here's a simplified example of my implementation: import { RouterProps } from "instantsearch.js/es/middlewares/createRouterMiddleware";
import { IndexUiState, UiState } from "instantsearch.js/es/types/ui-state";
import { history } from "instantsearch.js/es/lib/routers";
const routing: RouterProps<UiState, UiState> = {
router: history(),
stateMapping: {
stateToRoute(uiState) {
const indexUiState = uiState[process.env.ALGOLIA_INDEX_NAME!];
const route: Record<string, any> = {};
// ...
if (indexUiState.page) {
route.page = indexUiState.page;
}
return route;
},
routeToState(routeState) {
const indexUiState: IndexUiState = {};
// ERROR: `page` field always missing from `routeState`!
if (routeState.page) {
indexUiState.page = routeState.page;
}
// ...
return {
[process.env.ALGOLIA_INDEX_NAME!]: indexUiState,
};
}
}
} This is used in jsx: import algoliasearch from "algoliasearch/lite";
import { InstantSearch } from "react-instantsearch-hooks-web"; <InstantSearch
searchClient={searchClient}
indexName={process.env.ALGOLIA_INDEX_NAME}
routing={routing}
>
<Search />
</InstantSearch> |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
For a parameter to stay in the URL, a widget needs to own that parameter. In this case, page is owned by Pagination or usePagination. That widget must be rendered on the page |
Beta Was this translation helpful? Give feedback.
I created a codesandbox and it worked fine there, so I'm not linking to it. What I realized was that I'm running the search React app as a custom block inside WordPress, and it's the WP that removes the query param. WP uses
?page=
for it's own blog post pagination and since there is no need for pagination on that page, it removes the parameter. I have to try and figure out if there's a way to retain "reserved" parameters in WP.Thanks for your help anyway!