@@ -39,8 +39,8 @@ export const Routes = {
3939
4040export type QueryParamTypeMapping { // <-- * use type
4141 [Routes.Route1]: {
42- param1: {[type in PagesType ]?: boolean};
43- param2 : PagesType ;
42+ param1: {[type in RoutesType ]?: boolean};
43+ param2 : RoutesType ;
4444 param3 : number [];
4545 param4 : string ;
4646 },
@@ -168,27 +168,73 @@ export const { NavQueryContext, useNavQueryParams } = creator(
168168### Usage(TS/JS Users)
169169``` tsx
170170
171+ // (example using react router v6, but could be adapted for other routing solutions)
171172const router = createBrowserRouter ([ // from react router v6
172173 {
173- path: " /" ,
174- element: <ReadingParams />
175- },
176- {
177- path: " /route1" ,
178- element: <NavigatingAndSettingParams />
179- }
174+ path: " /" ,
175+ element: <RouteBasePage />,
176+ children: [{
177+ path: " /" ,
178+ element: <ReadingParams />,
179+ index: true ,
180+ },
181+ {
182+ path: " /route1" ,
183+ element: <NavigatingAndSettingParams />
184+ }]
185+ }
180186])
181187
188+ // In the root App component...
182189function App(){
183190 return (
184191 <div className = " App" >
185- <NavQueryContext.Provider value = { {}} >
186192 <RouterProvider router = { router } /> { /* from react router v6 */ }
187- </NavQueryContext.Provider >
188193 </div >
189194 );
190195}
191196
197+
198+
199+ // In a child component of the app component need to create an adapter object that tells this package how to manage history and location
200+ // need access to the navigate / location data from react router v6 package
201+
202+ import { Adapter } from " react-nav-query-params" ; // <-- TS users only
203+ import { useLocation , useNavigate , Outlet } from " react-router-dom" ; // <-- react router v6 users only
204+
205+ function RouteBasePage(){
206+ // the following is specific to react-router-v6 and might look different for each routing solution,
207+ // but the adapter created needs to be provided in a similar manner into the NavQueryContext.Provider or else the useNavQueryParams hook will not work
208+ const location = useLocation (); // <-- for react-router, can only be called in a child component of RouterProvider
209+ const navigate = useNavigate (); // <-- for react-router, can only be called in a child component of RouterProvider
210+
211+ const adapter = useMemo (() => { // <-- JS users, const adapter = useMemo<Adapter>(() => { //<-- TS users
212+ return {
213+ location: location ,
214+ pushLocation : (l ) => { // the type of 'l' here is Adapter["location"] for TS users
215+ if (l .search !== null || l .search !== undefined )
216+ navigate (" ?" + l .search , { replace: false });
217+ },
218+ replaceLocation : (l ) => {
219+ if (l .search !== null || l .search !== undefined )
220+ navigate (" ?" + l .search , { replace: true });
221+ },
222+ };
223+ }, [location , navigate ]);
224+
225+ return (
226+ <NavQueryContext.Provider
227+ value = { {
228+ adapter: adapter ,
229+ }}
230+ >
231+ <div >
232+ <Outlet />
233+ </div >
234+ </NavQueryContext.Provider >
235+ );
236+ }
237+
192238// A component where you read the query params
193239function ReadingParams(){
194240 const { getQueryParams : getQueryParamsRoute1, clearQueryParams : clearQueryParamsRoute1 } = useNavQueryParams (Routes .Route1 );
@@ -235,7 +281,7 @@ function NavigatingAndSettingParams() {
235281
236282 return (
237283 <div >
238- <button onClick = { () => navigate (" route3 " + queryString )} >Click Me</button >
284+ <button onClick = { () => navigate (" /route1 " + queryString )} >Click Me</button >
239285 </div >
240286 );
241287}
0 commit comments