-
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
example: rename rematch -> react-router.
- Loading branch information
1 parent
9aacae6
commit 7dcc975
Showing
62 changed files
with
293 additions
and
640 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
body { | ||
margin: 0; | ||
padding: 0; | ||
font-size: 12px; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,26 @@ | ||
import React, { Component } from 'react'; | ||
import React from 'react'; | ||
import logo from './logo.svg'; | ||
import styles from './App.module.less'; | ||
import markdown from './App.md'; | ||
|
||
class App extends Component { | ||
render() { | ||
return ( | ||
<div className={styles.App}> | ||
<header className={styles.AppHeader}> | ||
<img src={logo} className={styles.AppLogo} alt="logo" /> | ||
<p> | ||
Edit <code>src/app/App.js</code> and save to reload. | ||
</p> | ||
<p>{markdown}</p> | ||
<div> | ||
<a className={styles.AppLink} href="https://reactjs.org" target="_blank" rel="noopener noreferrer"> | ||
Learn React | ||
</a> | ||
<a className={styles.AppLink} href="https://github.com/kktjs/kkt" target="_blank" rel="noopener noreferrer"> | ||
Learn KKT | ||
</a> | ||
</div> | ||
</header> | ||
</div> | ||
); | ||
} | ||
export default function App() { | ||
return ( | ||
<div className={styles.App}> | ||
<header className={styles.AppHeader}> | ||
<img src={logo} className={styles.AppLogo} alt="logo" /> | ||
<p> | ||
Edit <code>src/app/App.js</code> and save to reload. | ||
</p> | ||
<p>{markdown}</p> | ||
<div> | ||
<a className={styles.AppLink} href="https://reactjs.org" target="_blank" rel="noopener noreferrer"> | ||
Learn React | ||
</a> | ||
<a className={styles.AppLink} href="https://github.com/kktjs/kkt" target="_blank" rel="noopener noreferrer"> | ||
Learn KKT | ||
</a> | ||
</div> | ||
</header> | ||
</div> | ||
); | ||
} | ||
|
||
export default App; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { useRoutes } from 'react-router-dom'; | ||
import { AuthProvider } from './components/AuthProvider'; | ||
import { routes } from './routers'; | ||
|
||
export default function App() { | ||
const element = useRoutes(routes); | ||
return <AuthProvider>{element}</AuthProvider>; | ||
} |
56 changes: 56 additions & 0 deletions
56
example/react-router/src/components/AuthProvider/index.jsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import { createContext, useState, useContext } from 'react'; | ||
import { useLocation, Navigate } from 'react-router-dom'; | ||
|
||
let AuthContext = createContext(null); | ||
/** | ||
* This represents some generic auth provider API, like Firebase. | ||
*/ | ||
const fakeAuthProvider = { | ||
isAuthenticated: false, | ||
signin(callback) { | ||
fakeAuthProvider.isAuthenticated = true; | ||
setTimeout(callback, 100); // fake async | ||
}, | ||
signout(callback) { | ||
fakeAuthProvider.isAuthenticated = false; | ||
setTimeout(callback, 100); | ||
}, | ||
}; | ||
|
||
export function AuthProvider({ children }) { | ||
let [user, setUser] = useState(null); | ||
|
||
let signin = ({ username, password }, callback) => | ||
fakeAuthProvider.signin(() => { | ||
setUser(username); | ||
callback(); | ||
}); | ||
|
||
let signout = (callback) => { | ||
return fakeAuthProvider.signout(() => { | ||
setUser(null); | ||
callback(); | ||
}); | ||
}; | ||
|
||
return <AuthContext.Provider value={{ user, signin, signout }}>{children}</AuthContext.Provider>; | ||
} | ||
|
||
export function useAuth() { | ||
return useContext(AuthContext); | ||
} | ||
|
||
export function RequireAuth({ children }) { | ||
let auth = useAuth(); | ||
let location = useLocation(); | ||
|
||
if (!auth.user) { | ||
// Redirect them to the /login page, but save the current location they were | ||
// trying to go to when they were redirected. This allows us to send them | ||
// along to that page after they login, which is a nicer user experience | ||
// than dropping them off on the home page. | ||
return <Navigate to="/login" state={{ from: location }} replace />; | ||
} | ||
|
||
return children; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export default function NoMatch() { | ||
return <div>Error.</div>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { Link } from 'react-router-dom'; | ||
import styles from './index.module.less'; | ||
import { useAuth } from '../AuthProvider'; | ||
|
||
export default function GlobalHeader() { | ||
let auth = useAuth(); | ||
// let navigate = useNavigate(); | ||
// console.log('auth:', auth, navigate) | ||
return ( | ||
<div className={styles.header}> | ||
<div className={styles.inner}> | ||
<Link to="/"> 首页 </Link> | ||
<div className={styles.right}> | ||
{auth.user ? <Link to="/"> 你好!{auth.user || '-'} </Link> : <Link to="/login"> 登录 </Link>} | ||
{auth.user && <Link to="/login"> 退出登录 </Link>} | ||
</div> | ||
</div> | ||
</div> | ||
); | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import styles from './index.module.less'; | ||
|
||
export default function SiderMenu() { | ||
return <div className={styles.wapper}>菜单</div>; | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import React from 'react'; | ||
import ReactDOM from 'react-dom'; | ||
import { BrowserRouter } from 'react-router-dom'; | ||
import App from './App'; | ||
import './styles/index.less'; | ||
|
||
ReactDOM.render( | ||
<React.StrictMode> | ||
<BrowserRouter> | ||
<App /> | ||
</BrowserRouter> | ||
</React.StrictMode>, | ||
document.getElementById('root'), | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import React from 'react'; | ||
import { Outlet } from 'react-router-dom'; | ||
import SiderMenu from '../components/SiderMenu'; | ||
import GlobalHeader from '../components/GlobalHeader'; | ||
import styles from './BasicLayout.module.less'; | ||
|
||
export default function BasicLayout() { | ||
return ( | ||
<div className={styles.wapper}> | ||
<SiderMenu /> | ||
<div className={styles.container}> | ||
<GlobalHeader /> | ||
<Outlet /> | ||
</div> | ||
</div> | ||
); | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { lazy, Suspense } from 'react'; | ||
import BasicLayout from './layouts/BasicLayout'; | ||
import LoginPage from './routes/login'; | ||
import NoMatch from './routes/NoMatch'; | ||
|
||
const Loadable = (Component) => (props) => | ||
( | ||
<Suspense fallback={<div>Loading...</div>}> | ||
<Component {...props} /> | ||
</Suspense> | ||
); | ||
|
||
const Home = Loadable(lazy(() => import('./routes/home'))); | ||
const Help = Loadable(lazy(() => import('./routes/help/dashboard'))); | ||
|
||
export const routes = [ | ||
{ | ||
path: '/login', | ||
element: <LoginPage />, | ||
}, | ||
{ | ||
path: '/', | ||
element: <BasicLayout />, | ||
children: [ | ||
{ | ||
index: true, | ||
element: <Home />, | ||
}, | ||
{ | ||
path: '/home', | ||
element: <Home />, | ||
}, | ||
{ | ||
path: '/help', | ||
element: <Help />, | ||
}, | ||
{ path: '*', element: <NoMatch /> }, | ||
], | ||
}, | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { Link } from 'react-router-dom'; | ||
|
||
export default function NoMatch() { | ||
return ( | ||
<div> | ||
<h2>It looks like you're lost...</h2> | ||
<p> | ||
<Link to="/">Go to the home page</Link> | ||
</p> | ||
</div> | ||
); | ||
} |
Oops, something went wrong.