react-router-dom#To TypeScript Examples

The following examples show how to use react-router-dom#To. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: useRouter.ts    From ant-simple-draw with MIT License 6 votes vote down vote up
useRouter = () => {
  const params = useParams();
  const location = useLocation();
  const navigate = useNavigate();
  const replace = (to: To) => {
    navigate(to, { replace: true });
  };
  return useMemo(() => {
    return {
      push: navigate,
      back: navigate(-1),
      replace,
      pathname: location.pathname,
      // 获取地址栏的参数,将字符串转为json
      // 例如: /:topic?sort=popular -> { topic: "react", sort: "popular" }
      query: {
        ...queryString.parse(location.search),
        ...params,
      },
      location,
    };
  }, [params, location]);
}