blob: 9eb26c20774b48d4f5dc427f91999bc167dc3ee3 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
import { useRouter } from 'next/router';
import { useEffect } from 'react';
export type RouterQuery = {
param: string;
value: string;
};
export type UseRedirectionProps = {
/**
* The router query.
*/
query: RouterQuery;
/**
* The redirection url.
*/
redirectTo: string;
};
/**
* Redirect to another url when router query match the given parameters.
*
* @param {UseRedirectionProps} props - The redirection parameters.
*/
const useRedirection = ({ query, redirectTo }: UseRedirectionProps) => {
const router = useRouter();
useEffect(() => {
if (router.query[query.param] === query.value) router.push(redirectTo);
}, [query, redirectTo, router]);
};
export default useRedirection;
|