parent
7364f33785
commit
928d1c2888
@ -0,0 +1,33 @@
|
||||
import { useCallback } from 'react';
|
||||
|
||||
import { useRouter } from 'next/navigation';
|
||||
|
||||
interface INavParams {
|
||||
path: string;
|
||||
query?: Record<string, any>;
|
||||
}
|
||||
|
||||
export default function useNav() {
|
||||
const router = useRouter();
|
||||
|
||||
const push = useCallback(
|
||||
({ path, query }: INavParams) => {
|
||||
let queryStr = '';
|
||||
if (query && Object.keys(query).length > 0) {
|
||||
const queryArr: string[] = [];
|
||||
Object.entries(query).forEach(([key, val]) => {
|
||||
queryArr.push(`${key}=${encodeURIComponent(val)}`);
|
||||
});
|
||||
queryStr += `?${queryArr.join('&')}`;
|
||||
}
|
||||
if (process.env.NODE_ENV === 'production') {
|
||||
window.location.href = `${path}.html${queryStr}`;
|
||||
} else {
|
||||
router.push(`${path}${queryStr}`);
|
||||
}
|
||||
},
|
||||
[router],
|
||||
);
|
||||
|
||||
return { push };
|
||||
}
|
Loading…
Reference in new issue