2
0

Router.tsx 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. import { Switch, Route, Redirect, HashRouter } from 'react-router-dom';
  2. import routerConfig from './Config';
  3. import Layout from '../components/layout/Layout';
  4. import { useContext } from 'react';
  5. import { authContext } from '../context/Auth';
  6. /**
  7. * Global responsible for global effect
  8. * Layout responsible for ui view
  9. *
  10. */
  11. const RouterWrapper = () => {
  12. const { isAuth } = useContext(authContext);
  13. return (
  14. <HashRouter>
  15. <Layout>
  16. <Switch>
  17. {routerConfig.map(v => (
  18. <Route
  19. exact
  20. key={v.path}
  21. path={v.path}
  22. render={() => {
  23. const Page = v.component;
  24. return isAuth || !v.auth ? (
  25. <Page />
  26. ) : (
  27. <Redirect
  28. to={{
  29. pathname: '/connect',
  30. }}
  31. />
  32. );
  33. }}
  34. />
  35. ))}
  36. <Route
  37. render={() => {
  38. return (
  39. <Redirect
  40. to={{
  41. pathname: '/connect',
  42. }}
  43. />
  44. );
  45. }}
  46. ></Route>
  47. </Switch>
  48. </Layout>
  49. </HashRouter>
  50. );
  51. };
  52. export default RouterWrapper;