Commit c035a5fc authored by 陈帅's avatar 陈帅

Simple performance optimization

parent 123401a9
import React, { Component } from 'react'; import React, { Component } from 'react';
import { MiniArea } from '../Charts'; import { MiniArea } from '../Charts';
import NumberInfo from '../NumberInfo'; import NumberInfo from '../NumberInfo';
...@@ -34,18 +33,18 @@ export default class ActiveChart extends Component { ...@@ -34,18 +33,18 @@ export default class ActiveChart extends Component {
} }
loopData = () => { loopData = () => {
requestAnimationFrame(() => {
this.timer = setTimeout(() => { this.timer = setTimeout(() => {
this.setState( this.setState(
{ {
activeData: getActiveData(), activeData: getActiveData(),
}, },
() => { () => {
requestAnimationFrame(() => {
this.loopData(); this.loopData();
});
} }
); );
}, 1000); }, 1000);
});
}; };
render() { render() {
......
import React, { PureComponent, createElement } from 'react'; import React, { PureComponent, createElement } from 'react';
import pathToRegexp from 'path-to-regexp'; import pathToRegexp from 'path-to-regexp';
import { Breadcrumb, Tabs, Card } from 'antd'; import { Breadcrumb, Tabs, Card } from 'antd';
import memoizeOne from 'memoize-one';
import deepEqual from 'lodash.isequal';
import classNames from 'classnames'; import classNames from 'classnames';
import styles from './index.less'; import styles from './index.less';
import { urlToList } from '../_utils/pathTools'; import { urlToList } from '../_utils/pathTools';
const { TabPane } = Tabs; const { TabPane } = Tabs;
export function getBreadcrumb(breadcrumbNameMap, url) { export const getBreadcrumb = (breadcrumbNameMap, url) => {
let breadcrumb = breadcrumbNameMap[url]; let breadcrumb = breadcrumbNameMap[url];
if (!breadcrumb) { if (!breadcrumb) {
Object.keys(breadcrumbNameMap).forEach(item => { Object.keys(breadcrumbNameMap).forEach(item => {
...@@ -16,8 +18,14 @@ export function getBreadcrumb(breadcrumbNameMap, url) { ...@@ -16,8 +18,14 @@ export function getBreadcrumb(breadcrumbNameMap, url) {
}); });
} }
return breadcrumb || {}; return breadcrumb || {};
} };
export default class PageHeader extends PureComponent { export default class PageHeader extends PureComponent {
constructor(props) {
super(props);
this.conversionFromLocation = memoizeOne(this.conversionFromLocation, deepEqual);
}
state = { state = {
breadcrumb: null, breadcrumb: null,
}; };
...@@ -59,12 +67,11 @@ export default class PageHeader extends PureComponent { ...@@ -59,12 +67,11 @@ export default class PageHeader extends PureComponent {
// Generated according to props // Generated according to props
conversionFromProps = () => { conversionFromProps = () => {
const { breadcrumbList, breadcrumbSeparator, linkElement = 'a' } = this.props; const { breadcrumbList, breadcrumbSeparator, itemRender, linkElement = 'a' } = this.props;
return ( return (
<Breadcrumb className={styles.breadcrumb} separator={breadcrumbSeparator}> <Breadcrumb className={styles.breadcrumb} separator={breadcrumbSeparator}>
{breadcrumbList.map(item => { {breadcrumbList.map(item => {
const title = this.props.itemRender ? this.props.itemRender(item) : item.title; const title = itemRender ? itemRender(item) : item.title;
return ( return (
<Breadcrumb.Item key={item.title}> <Breadcrumb.Item key={item.title}>
{item.href {item.href
...@@ -84,17 +91,14 @@ export default class PageHeader extends PureComponent { ...@@ -84,17 +91,14 @@ export default class PageHeader extends PureComponent {
}; };
conversionFromLocation = (routerLocation, breadcrumbNameMap) => { conversionFromLocation = (routerLocation, breadcrumbNameMap) => {
const { breadcrumbSeparator, linkElement = 'a' } = this.props; const { breadcrumbSeparator, home, itemRender, linkElement = 'a' } = this.props;
// Convert the url to an array // Convert the url to an array
const pathSnippets = urlToList(routerLocation.pathname); const pathSnippets = urlToList(routerLocation.pathname);
// Loop data mosaic routing // Loop data mosaic routing
const extraBreadcrumbItems = pathSnippets.map((url, index) => { const extraBreadcrumbItems = pathSnippets.map((url, index) => {
console.log(url);
const currentBreadcrumb = getBreadcrumb(breadcrumbNameMap, url); const currentBreadcrumb = getBreadcrumb(breadcrumbNameMap, url);
const isLinkable = index !== pathSnippets.length - 1 && currentBreadcrumb.component; const isLinkable = index !== pathSnippets.length - 1 && currentBreadcrumb.component;
const name = this.props.itemRender const name = itemRender ? itemRender(currentBreadcrumb) : currentBreadcrumb.name;
? this.props.itemRender(currentBreadcrumb)
: currentBreadcrumb.name;
return currentBreadcrumb.name && !currentBreadcrumb.hideInBreadcrumb ? ( return currentBreadcrumb.name && !currentBreadcrumb.hideInBreadcrumb ? (
<Breadcrumb.Item key={url}> <Breadcrumb.Item key={url}>
{createElement( {createElement(
...@@ -113,7 +117,7 @@ export default class PageHeader extends PureComponent { ...@@ -113,7 +117,7 @@ export default class PageHeader extends PureComponent {
{ {
[linkElement === 'a' ? 'href' : 'to']: '/', [linkElement === 'a' ? 'href' : 'to']: '/',
}, },
this.props.home || 'Home' home || 'Home'
)} )}
</Breadcrumb.Item> </Breadcrumb.Item>
); );
......
...@@ -3,6 +3,8 @@ ...@@ -3,6 +3,8 @@
import React from 'react'; import React from 'react';
import { Layout } from 'antd'; import { Layout } from 'antd';
import DocumentTitle from 'react-document-title'; import DocumentTitle from 'react-document-title';
import { injectIntl } from 'react-intl';
import memoizeOne from 'memoize-one';
import { connect } from 'dva'; import { connect } from 'dva';
// import { Route, Redirect, Switch } from 'dva/router'; // import { Route, Redirect, Switch } from 'dva/router';
import { ContainerQuery } from 'react-container-query'; import { ContainerQuery } from 'react-container-query';
...@@ -26,7 +28,7 @@ const { AuthorizedRoute, check } = Authorized; ...@@ -26,7 +28,7 @@ const { AuthorizedRoute, check } = Authorized;
* @param {Object} menuData 菜单配置 * @param {Object} menuData 菜单配置
* @param {Object} routerData 路由配置 * @param {Object} routerData 路由配置
*/ */
const getBreadcrumbNameMap = (meun, router) => { const getBreadcrumbNameMap = memoizeOne((meun, router) => {
const routerMap = {}; const routerMap = {};
const mergeMeunAndRouter = meunData => { const mergeMeunAndRouter = meunData => {
meunData.forEach(meunItem => { meunData.forEach(meunItem => {
...@@ -38,7 +40,7 @@ const getBreadcrumbNameMap = (meun, router) => { ...@@ -38,7 +40,7 @@ const getBreadcrumbNameMap = (meun, router) => {
}; };
mergeMeunAndRouter(meun); mergeMeunAndRouter(meun);
return routerMap; return routerMap;
}; });
const query = { const query = {
'screen-xs': { 'screen-xs': {
...@@ -66,31 +68,38 @@ const query = { ...@@ -66,31 +68,38 @@ const query = {
}; };
class BasicLayout extends React.PureComponent { class BasicLayout extends React.PureComponent {
constructor(props) {
super(props);
const { routerData, menuData } = this.props;
this.breadcrumbNameMap = getBreadcrumbNameMap(menuData, routerData);
}
getContext() { getContext() {
const { location, routerData, menuData } = this.props; const { location } = this.props;
console.log(getBreadcrumbNameMap(menuData, routerData));
return { return {
location, location,
breadcrumbNameMap: getBreadcrumbNameMap(menuData, routerData), breadcrumbNameMap: this.breadcrumbNameMap,
}; };
} }
getPageTitle() { getPageTitle = pathname => {
const { routerData, location } = this.props;
const { pathname } = location;
let title = 'Ant Design Pro';
let currRouterData = null; let currRouterData = null;
const { intl } = this.props;
// match params path // match params path
Object.keys(routerData).forEach(key => { Object.keys(this.breadcrumbNameMap).forEach(key => {
if (pathToRegexp(key).test(pathname)) { if (pathToRegexp(key).test(pathname)) {
currRouterData = routerData[key]; currRouterData = this.breadcrumbNameMap[key];
} }
}); });
if (currRouterData && currRouterData.name) { if (!currRouterData) {
title = `${currRouterData.name} - Ant Design Pro`; return 'Ant Design Pro';
}
return title;
} }
const message = intl.formatMessage({
id: currRouterData.locale || currRouterData.name,
defaultMessage: currRouterData.name,
});
return `${message} - Ant Design Pro`;
};
getLayoutStyle = () => { getLayoutStyle = () => {
const { fixSiderbar, collapsed, layout } = this.props; const { fixSiderbar, collapsed, layout } = this.props;
...@@ -148,7 +157,7 @@ class BasicLayout extends React.PureComponent { ...@@ -148,7 +157,7 @@ class BasicLayout extends React.PureComponent {
silderTheme, silderTheme,
layout: PropsLayout, layout: PropsLayout,
children, children,
// match, location: { pathname },
} = this.props; } = this.props;
const isTop = PropsLayout === 'topmenu'; const isTop = PropsLayout === 'topmenu';
// const bashRedirect = this.getBashRedirect(); // const bashRedirect = this.getBashRedirect();
...@@ -190,9 +199,9 @@ class BasicLayout extends React.PureComponent { ...@@ -190,9 +199,9 @@ class BasicLayout extends React.PureComponent {
</Layout> </Layout>
</Layout> </Layout>
); );
const getPageTitle = memoizeOne(this.getPageTitle);
return ( return (
<DocumentTitle title={this.getPageTitle()}> <DocumentTitle title={getPageTitle(pathname)}>
<ContainerQuery query={query}> <ContainerQuery query={query}>
{params => ( {params => (
<Context.Provider value={this.getContext()}> <Context.Provider value={this.getContext()}>
...@@ -212,4 +221,4 @@ export default connect(({ global, setting }) => ({ ...@@ -212,4 +221,4 @@ export default connect(({ global, setting }) => ({
collapsed: global.collapsed, collapsed: global.collapsed,
layout: setting.layout, layout: setting.layout,
...setting, ...setting,
}))(BasicLayout); }))(injectIntl(BasicLayout));
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment