You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I remeber a tweet about react-query asking samples / ideas about how to handle the synchronization of the state with the URL, am i dreaming, i can't find it. I would be very interesting to have this with react-query.
I have develop something like that, in a declarative manner:
The hook to retrieve the state from the URL and sync the changes
// queryParams is of type GetAllAccounts
// page, size, sort, order and q are automaticaly manage, no need to declare it except to override it
const { queryParams, pagination, search, sort, filter, clear } = useAdvancedQueryParams<GetAllAccounts>({
params: {
offerId: {},
states: {
default: defaultStates,
map: {
params: 'states',
transform: (states) => toStatesOrNull<AccountState>(states),
}
}
}
});
The hook to retrieve accounts, useAccount, based on useQuery and the process state
// args is of type GetAllAccounts == queryParams
export function useAccounts(args: GetAllAccounts, config: UseQueryOptions<PaginatedAccounts> = {}) {
const {data: paginatedAccounts, ...rest} = useQuery<PaginatedAccounts>(
[accountsKey, args],
async () => {
const response = await getAccounts(args);
return extractPaginatedResponse(response);
},
config
)
const data = paginatedAccounts ?? emptyPaginatedList;
return {data, ...rest};
}
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
I remeber a tweet about react-query asking samples / ideas about how to handle the synchronization of the state with the URL, am i dreaming, i can't find it. I would be very interesting to have this with react-query.
I have develop something like that, in a declarative manner:
This code is extract from my account list page.
The state
The hook to retrieve the state from the URL and sync the changes
The hook to retrieve accounts, useAccount, based on useQuery and the process state
The hook to translate the state to human readable
can return a JSX or map the value
A generic component to display all the translations
<DisplayAdvancedQueryParams translations={translations} clear={clear}/>
A more advance state declaration
Beta Was this translation helpful? Give feedback.
All reactions