-
Notifications
You must be signed in to change notification settings - Fork 96
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Specify query param configurations in one place #14
Comments
Hi and thank you for the very well written issue. Sorry for the delay in my response! My current take is this can be solved in userland via hooks/context. For instance I have a The alternative is to create an import * as React from 'react';
import { useQueryParams } from 'use-query-params';
const AppQueryParamContext = React.createContext(undefined);
export function AppQueryParamProvider({ children }) {
/* [query, setQuery] */
const contextValue = useQueryParams({ foo: StringParam, bar: NumberParam });
return (
<AppQueryParamContext.Provider value={contextValue}>
{children}
</AppQueryParamContext.Provider>
);
}
export function useAppQueryParams() {
return React.useContext(AppQueryParamContext);
} Note I haven't tested this context approach, I'm not sure if it results in a render happening between param updates and the context updating, but it should work. |
Hi there, happy to say this has been added in v2.0.0-rc.0 via the |
The current API requires that you specify the structure of the query parameters in every component that wants to access params. Consider a large app with 1000 components that all need
x
,q
, andfilters
. You will see the following code 1000 times:This seems unnecessarily verbose since it is not like
x
will be aNumberParam
in one component, and anArrayParam
in another.Have you considered DRY'ing up the API by allowing this to be specified in a single location:
With this, all consumers could then simply do:
Benefits:
Cons:
What do you think?
Thanks for reading!
The text was updated successfully, but these errors were encountered: