-
-
Notifications
You must be signed in to change notification settings - Fork 359
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
Improve navigate()
#2197
Comments
Does it work with |
Let's re-open if it doesn't. |
It partially works, logical url changes as expected, |
I kinda like it, but it isn't explicitly part of the API contract (yet?). Anything that makes you feel like it should be? |
What do you mean ? Something like that
But still this does not change url in url bar. |
How about this? navigate({
search: {
someNewQuery: 'param',
removedQuery: null
}
}) navigate({
searchAll: {}, // remove all previous queries
search: {
someNewQuery: 'param',
}
}) // Full API
navigate({
pathname: '/some-path',
hash: 'some-hash',
searchAll: { filter: ['cars', 'computers']] }, // ?filter=cars&filter=computers
search: {
someExtraQuery: 'param',
}
})
// Backwards compatible
navigate('/hello?world=1') I'm hesitating about WDYT? Would you prefer this or still prefer |
Or maybe: // URL current: /products?filter=cars
// URL next: /products?someNewQuery=param
navigate({
// overwrite
searchAll: "",
// mutate
search: {
someNewQuery: 'param',
}
}) // URL current: /products?filter=cars
// URL next: /products?filter=cars&filter=computers
navigate({
// overwrite
searchAll: "filter=cars&filter=computers"
}) |
Also: // URL current: /products?filter=cars#price
// URL next: /products#price
navigate('?') // URL current: /products?filter=cars#price
// URL next: /products?filter=cars
navigate('#') Feel free to ignore my designing thinking, but definitely let me know if you think there is a neat thing about |
Here is how my import { NavigateOptions, useNavigate } from './useNavigate';
import { useCallback } from 'react';
import { usePageContext } from 'vike-react/usePageContext';
export const useSearchParams = (): [
URLSearchParams,
(params: string[][] | Record<string, string> | string | URLSearchParams) => void,
] => {
const { urlParsed } = usePageContext();
const navigate = useNavigate();
const searchParams = new URLSearchParams(urlParsed.search);
const setSearchParams = useCallback(
(
params: string[][] | Record<string, string> | string | URLSearchParams,
options?: NavigateOptions,
) => {
const newParams = new URLSearchParams(params);
navigate(`?${newParams.toString()}`, options);
},
[navigate],
);
return [searchParams, setSearchParams];
}; |
I see, I like it. It could be nice to ship it with Note that your implementation erases the - navigate(`?${newParams.toString()}`, options);
+ // Preserves hash
+ navigate(`?${newParams.toString()}${urlParsed.hashOriginal ?? ''}`, options); Also, wouldn't the following be simpler? Isn't - import { NavigateOptions, useNavigate } from './useNavigate';
+ import { navigate } from 'vike/client/router';
import { useCallback } from 'react';
import { usePageContext } from 'vike-react/usePageContext';
export const useSearchParams = (): [
URLSearchParams,
(params: string[][] | Record<string, string> | string | URLSearchParams) => void,
] => {
const { urlParsed } = usePageContext();
- const navigate = useNavigate();
const searchParams = new URLSearchParams(urlParsed.search);
const setSearchParams = useCallback(
(
params: string[][] | Record<string, string> | string | URLSearchParams,
options?: NavigateOptions,
) => {
const newParams = new URLSearchParams(params);
navigate(`?${newParams.toString()}`, options);
},
- [navigate],
+ [],
);
return [searchParams, setSearchParams];
}; |
Agree, it is better to keep hash in general |
navigate()
Description
navigate("")
Expected behaviour - remove all search params (It was working like that in previous versions)Environment
The text was updated successfully, but these errors were encountered: