How to make initialUiState work with stateMapping? #5350
-
Is there some rule or what could I probably be missing here? When I remove But as soon as I put back Can you please help shed light on this and what could I probably be missing to make those two work. We're using stateMapping for SEO friendly URLs and we've come to a point where we would like to show initially on load sorted by the most downloaded. <InstantSearch
searchClient={client}
indexName="instant_search"
initialUiState={{
instant_search: {
query: "polar"
}
}}
onStateChange={onStateChange}
routing={{
router: history({
getLocation() {
if (typeof window === "undefined") {
return (new URL(url!) as unknown) as Location;
}
return window.location;
}
})
stateMapping: {
stateToRoute(uiState) {
const indexUiState = uiState["instant_search"] || {};
return {
query: indexUiState.query,
page: indexUiState.page,
brands: indexUiState.refinementList?.brand,
category: indexUiState.menu?.categories
};
},
routeToState(routeState) {
return {
instant_search: {
query: routeState.query,
page: routeState.page,
menu: {
categories: routeState.category
},
refinementList: {
brand: routeState.brands
}
}
};
}
}
}}
>
...
</InstantSearch> PS: I tried to set this up in a codesandbox here but couldn't quite get it to work. |
Beta Was this translation helpful? Give feedback.
Replies: 0 comments 5 replies
-
Both routing and initialUiState act on the same key of data. We indeed should have some kind of warning or error when they overlap and only one of the two is taken in account. Can you explain further what your use case of the initial query is that couldn't be derived from the URL? |
Beta Was this translation helpful? Give feedback.
Both routing and initialUiState act on the same key of data. We indeed should have some kind of warning or error when they overlap and only one of the two is taken in account.
Can you explain further what your use case of the initial query is that couldn't be derived from the URL?