Check umi documentation for more configuration
Can be used to obtain current routing information,
import { history } from 'umi';// The number of entities in the history stackconsole.log(history.length);// The action of the current history jump has three types: PUSH, REPLACE and POPconsole.log(history.action);// location object, including pathname, search and hashconsole.log(history.location.pathname);console.log(history.location.search);console.log(history.location.hash);
Can be used for routing jumps,
import { history } from 'umi';// Jump to the specified routehistory.push('/list');// Jump to the specified route with parametershistory.push('/list?a=b');history.push({pathname: '/list',query: {a: 'b',},});// Jump to the previous routehistory.goBack();
It can also be used for routing monitoring,
import { history } from 'umi';const unlisten = history.listen((location, action) => {console.log(location.pathname);});unlisten();
Link components, for example:
import { Link } from 'umi';export default () => {return (<div>{/* Click to jump to the specified /about route */}<Link to="/about">About</Link>{/* Click to jump to the specified /courses route,With query {sort:'name'}*/}<Link to="/courses?sort=name">Courses</Link>{/* Click to jump to the specified /list route,With query: {sort:'name'}With hash:'the-hash'With state: {fromDashboard: true}*/}<Linkto={{pathname: '/list',search: '?sort=name',hash: '#the-hash',state: { fromDashboard: true },}}>List</Link>{/* Click to jump to the specified /profile route,With all the parameters on the current location*/}<Linkto={(location) => {return { ...location, pathname: '/profile' };}}/>{/* Click to jump to the specified /courses route,But it will replace the records in the current history stack*/}<Link to="/courses" replace />{/*innerRef allows you to get the basic components (here should be a tag or null)*/}<Linkto="/courses"innerRef={(node) => {// `node` refers to the mounted DOM element// or null when unmounted}}/></div>);};
Special version of <Link />
. When the specified route (to=specified route
) hits, a specific pattern can be attached.
import { NavLink } from 'umi';export default () => {return (<div>{/* Equivalent to Link */}<NavLink to="/about">About</NavLink>{/* When the current route is /faq, attach class selected */}<NavLink to="/faq" activeClassName="selected">FAQs</NavLink>{/* When the current route is /faq, attach style */}<NavLinkto="/faq"activeStyle={{fontWeight: 'bold',color: 'red',}}>FAQs</NavLink>{/* When the current route completely matches /profile, attach the class */}<NavLink exact to="/profile" activeClassName="selected">Profile</NavLink>{/* When the current route is /profile/, attach class */}<NavLink strict to="/profile/" activeClassName="selected">Profile</NavLink>{/* When the current route is /profile and query contains name, attach class */}<NavLinkto="/profile"exactactiveClassName="selected"isActive={(match, location) => {if (!match) {return false;}return location.search.includes('name');}}>Profile</NavLink></div>);};
hooks, get the history
object
import { useHistory } from 'umi';export default () => {const history = useHistory();return (<div><ul><li>history: {history.action}</li></ul></div>);};
hooks, get the location
object
import { useLocation } from 'umi';export default () => {const location = useLocation();return (<div><ul><li>location: {location.pathname}</li></ul></div>);};
hooks, get the params
object. The params
object is the parameter key-value pair in the dynamic route (for example: /users/:id
).
import { useParams } from 'umi';export default () => {const params = useParams();return (<div><ul><li>params: {JSON.stringify(params)}</li></ul></div>);};
Get the matching information of the current route.
import { useRouteMatch } from 'umi';export default () => {const match = useRouteMatch();return (<div><ul><li>match: {JSON.stringify(match.params)}</li></ul></div>);};