mirror of
https://github.com/kingomarnajjar/flex-template-web.git
synced 2026-07-26 06:47:17 +10:00
Add page structure and universal routing
This commit is contained in:
parent
6f93d85a7d
commit
322ec599b8
10 changed files with 81 additions and 14 deletions
|
|
@ -1,3 +0,0 @@
|
|||
.root {
|
||||
color: red;
|
||||
}
|
||||
|
|
@ -1,6 +0,0 @@
|
|||
import React from 'react';
|
||||
import css from './App.css';
|
||||
|
||||
export default () => (
|
||||
<div className={css.root}>hello, world</div>
|
||||
);
|
||||
18
src/Routes.js
Normal file
18
src/Routes.js
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
import React from 'react';
|
||||
import { Match, Miss, Redirect } from 'react-router';
|
||||
import * as pages from './pages';
|
||||
|
||||
const RedirectTestPage = () => (
|
||||
<Redirect to="/" />
|
||||
);
|
||||
|
||||
const Routes = () => (
|
||||
<div>
|
||||
<Match exactly pattern="/" component={ pages.Home } />
|
||||
<Match exactly pattern="/search" component={ pages.Search } />
|
||||
<Match exactly pattern="/home" component={ RedirectTestPage } />
|
||||
<Miss component={ pages.NotFound } />
|
||||
</div>
|
||||
);
|
||||
|
||||
export default Routes;
|
||||
11
src/components/Page/Page.js
Normal file
11
src/components/Page/Page.js
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
import React from 'react';
|
||||
|
||||
export default (props) => {
|
||||
const { className, title, children } = props;
|
||||
return (
|
||||
<div className={ className }>
|
||||
<h1>{ title }</h1>
|
||||
{ children }
|
||||
</div>
|
||||
);
|
||||
};
|
||||
5
src/components/index.js
Normal file
5
src/components/index.js
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
import Page from './Page/Page';
|
||||
|
||||
export {
|
||||
Page
|
||||
};
|
||||
15
src/index.js
15
src/index.js
|
|
@ -1,18 +1,23 @@
|
|||
import React from 'react';
|
||||
import ReactDOM from 'react-dom';
|
||||
import { renderToString } from 'react-dom/server';
|
||||
import App from './App';
|
||||
import { BrowserRouter, ServerRouter } from 'react-router';
|
||||
import Routes from './Routes';
|
||||
import './index.css';
|
||||
|
||||
if (typeof window !== 'undefined') {
|
||||
ReactDOM.render(
|
||||
<App />,
|
||||
<BrowserRouter>
|
||||
<Routes />
|
||||
</BrowserRouter>,
|
||||
document.getElementById('root')
|
||||
);
|
||||
}
|
||||
|
||||
export default function render(url, serverContext) {
|
||||
// TODO: Use ServerRouter from react-router
|
||||
console.log(`src/index.js: render() url: ${url}, serverContext: ${serverContext}`);
|
||||
return renderToString(<App />);
|
||||
return renderToString(
|
||||
<ServerRouter location={ url } context={ serverContext }>
|
||||
<Routes />
|
||||
</ServerRouter>
|
||||
);
|
||||
}
|
||||
|
|
|
|||
9
src/pages/Home.js
Normal file
9
src/pages/Home.js
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
import React from 'react';
|
||||
import { Link } from 'react-router';
|
||||
import { Page } from '../components';
|
||||
|
||||
export default () => (
|
||||
<Page title="Index page">
|
||||
<Link to="/search">search page</Link>
|
||||
</Page>
|
||||
);
|
||||
9
src/pages/NotFound.js
Normal file
9
src/pages/NotFound.js
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
import React from 'react';
|
||||
import { Link } from 'react-router';
|
||||
import { Page } from '../components';
|
||||
|
||||
export default () => (
|
||||
<Page title="Page not found">
|
||||
<Link to="/">index page</Link>
|
||||
</Page>
|
||||
);
|
||||
10
src/pages/Search.js
Normal file
10
src/pages/Search.js
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
import React from 'react';
|
||||
import { Link } from 'react-router';
|
||||
import { Page } from '../components';
|
||||
|
||||
export default () => (
|
||||
<Page title="Search page">
|
||||
<Link to="/">index page</Link><br />
|
||||
<Link to="/home">home page</Link>
|
||||
</Page>
|
||||
);
|
||||
9
src/pages/index.js
Normal file
9
src/pages/index.js
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
import Home from './Home';
|
||||
import Search from './Search';
|
||||
import NotFound from './NotFound';
|
||||
|
||||
export {
|
||||
Home,
|
||||
Search,
|
||||
NotFound,
|
||||
};
|
||||
Loading…
Add table
Reference in a new issue