開發 React + React Router 項目時,想實現一個路由跳轉時、滑動切換路由頁面的效果。react
提着兩把西瓜刀從 GitHub 一路砍到 Stack Overflow,楞是沒找到一個簡單易用的解決方案。git
有人可能會說,不有官方方案 react-transition-group
嗎?github
其實 react-transition-group
是一個提供基礎功能的庫,它給了菜、給了肉、給了油,但並非端上來一盤能夠直接開動的菜。shell
想要一個很常見的、頁面滑來滑去的路由切換效果,也沒找到一個完美的方案,得本身去封裝。react-router
React 社區太慘了,這麼簡單的事,這麼多年了居然沒人管,只好本身上了。dom
react-slide-routes 🏄♂ide
react-slide-routes
用於實現下面這種路由切換的效果code
安裝component
yarn add react-slide-routes
複製代碼
使用
import SlideRoutes from 'react-slide-routes';
import { Route, useLocation } from 'react-router-dom';
const App = () => {
const location = useLocation();
return (
<SlideRoutes location={location}>
<Route path="/" component={Home} exact />
<Route path="/about" component={About} />
</SlideRoutes>
);
};
複製代碼
是的,就是這樣,用 SlideRoutes
代替 Switch
便可,再沒別的幺蛾子了。
我來鵝城只辦三件事:簡單、簡單、仍是 TMD 簡單。
react-slide-routes
, the easiest way to slide React routes.
沒了。