Skip to content

Commit

Permalink
7. Add layout
Browse files Browse the repository at this point in the history
  • Loading branch information
sorrycc committed Dec 19, 2016
1 parent 85a7b5a commit 94788e5
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 16 deletions.
28 changes: 28 additions & 0 deletions src/components/MainLayout/Header.js
@@ -0,0 +1,28 @@
import React from 'react';
import { Menu, Icon } from 'antd';
import { Link } from 'dva/router';

function Header({ location }) {
return (
<Menu
selectedKeys={[location.pathname]}
mode="horizontal"
theme="dark"
>
<Menu.Item key="/users">
<Link to="/users"><Icon type="bars" />Users</Link>
</Menu.Item>
<Menu.Item key="/">
<Link to="/"><Icon type="home" />Home</Link>
</Menu.Item>
<Menu.Item key="/404">
<Link to="/page-you-dont-know"><Icon type="frown-circle" />404</Link>
</Menu.Item>
<Menu.Item key="/antd">
<a href="https://github.com/dvajs/dva" target="_blank">dva</a>
</Menu.Item>
</Menu>
);
}

export default Header;
16 changes: 16 additions & 0 deletions src/components/MainLayout/MainLayout.css
@@ -0,0 +1,16 @@

.normal {
display: flex;
flex-direction: column;
height: 100%;
}

.content {
flex: 1;
display: flex;
}

.main {
padding: 0 8px;
flex: 1 0 auto;
}
18 changes: 18 additions & 0 deletions src/components/MainLayout/MainLayout.js
@@ -0,0 +1,18 @@
import React from 'react';
import styles from './MainLayout.css';
import Header from './Header';

function MainLayout({ children, location }) {
return (
<div className={styles.normal}>
<Header location={location} />
<div className={styles.content}>
<div className={styles.main}>
{children}
</div>
</div>
</div>
);
}

export default MainLayout;
21 changes: 12 additions & 9 deletions src/routes/IndexPage.js
@@ -1,17 +1,20 @@
import React from 'react';
import { connect } from 'dva';
import styles from './IndexPage.css';
import MainLayout from '../components/MainLayout/MainLayout';

function IndexPage() {
function IndexPage({ location }) {
return (
<div className={styles.normal}>
<h1 className={styles.title}>Yay! Welcome to dva!</h1>
<div className={styles.welcome} />
<ul className={styles.list}>
<li>To get started, edit <code>src/index.js</code> and save to reload.</li>
<li><a href="https://github.com/dvajs/dva-docs/blob/master/v1/en-us/getting-started.md">Getting Started</a></li>
</ul>
</div>
<MainLayout location={location}>
<div className={styles.normal}>
<h1 className={styles.title}>Yay! Welcome to dva!</h1>
<div className={styles.welcome} />
<ul className={styles.list}>
<li>To get started, edit <code>src/index.js</code> and save to reload.</li>
<li><a href="https://github.com/dvajs/dva-docs/blob/master/v1/en-us/getting-started.md">Getting Started</a></li>
</ul>
</div>
</MainLayout>
);
}

Expand Down
8 changes: 5 additions & 3 deletions src/routes/Users.css
@@ -1,3 +1,5 @@

.normal {
}

.normal {
width: 900px;
margin: 3em auto 0;
}
11 changes: 7 additions & 4 deletions src/routes/Users.js
Expand Up @@ -2,12 +2,15 @@ import React from 'react';
import { connect } from 'dva';
import styles from './Users.css';
import UsersComponent from '../components/Users/Users';
import MainLayout from '../components/MainLayout/MainLayout';

function Users() {
function Users({ location }) {
return (
<div className={styles.normal}>
<UsersComponent />
</div>
<MainLayout location={location}>
<div className={styles.normal}>
<UsersComponent />
</div>
</MainLayout>
);
}

Expand Down

6 comments on commit 94788e5

@Justinnng
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

selectedKeys={[location.pathname]}, 这里的location.pathname是"/",麻烦问下是为什么?

@BoyInWindows
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Justinnng 我也遇到过,因为编写过程中少一个 location 导致让它变成 window.location.pathname

// 这是我总结的 location 传输路径
<Route
  IndexPage/Users({ location })
    <MainLayout location={location}
      function MainLayout({ children, location })
        <Header location={location}
          function Header({ location })
            <Menu selectedKeys={[location.pathname]}

@ZH4L
Copy link

@ZH4L ZH4L commented on 94788e5 Jan 18, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

我一直报....

Warning: React.createElement: type is invalid -- expected a string (for built-in components) or a class/function (for composite components) but got: object. You likely forgot to export your component from the file it's defined in. Check the render method of `IndexPage`.

@cronaldoyang
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

User.js中自己漏写了一个location导致最后出现TypeError: Cannot read property 'pathname' of undefined“”。还是要仔细

@mwangli
Copy link

@mwangli mwangli commented on 94788e5 Sep 4, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

export your component
export your component

@QuangMing
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

每个page引入layout 不是很麻烦吗,放到router下很方便

Please sign in to comment.