提交 a5da6143 authored 作者: vipcxj's avatar vipcxj

避免组件频繁unmount和mount

上级 f97da3cf
......@@ -15,33 +15,19 @@ const connect = (modelCreator, { app, mapStateToProps, mapDispatchToProps, merge
const { name, model } = modelCreator();
this.name = name;
this.model = model;
}
componentWillMount() {
(app || this.props.app || getApp()).model(this.model);
}
componentWillUnmount() {
(app || this.props.app || getApp()).unmodel(this.model.namespace);
}
render() {
const { children, ...rest } = this.props;
const name = this.name;
const model = this.model;
const mapState = (state) => {
const props = mapStateToProps ? mapStateToProps(state) : {};
props[name] = state[model.namespace];
props[namespaceVar || 'namespace'] = model.namespace;
const pps = mapStateToProps ? mapStateToProps(state) : {};
pps[name] = state[model.namespace];
pps[namespaceVar || 'namespace'] = model.namespace;
if (state.loading) {
props.loading = state.loading;
props.loading.model = props.loading.models[model.namespace];
props.loading.effect = flow(
pps.loading = state.loading;
pps.loading.model = pps.loading.models[model.namespace];
pps.loading.effect = flow(
pickBy((v, k) => startsWith(`${model.namespace}/`, k)),
mapKeys(k => k.slice(model.namespace.length + 1)),
)(props.loading.effects);
)(pps.loading.effects);
}
return props;
return pps;
};
const mapDispatch = (dispatch) => {
const extras = mapDispatchToProps ? mapDispatchToProps(dispatch) : {};
......@@ -54,11 +40,22 @@ const connect = (modelCreator, { app, mapStateToProps, mapDispatchToProps, merge
},
};
};
const Output = dvaConnect(mapState, mapDispatch, mergeProps, options)(Comp);
this.Output = dvaConnect(mapState, mapDispatch, mergeProps, options)(Comp);
}
componentWillMount() {
(app || this.props.app || getApp()).model(this.model);
}
componentWillUnmount() {
(app || this.props.app || getApp()).unmodel(this.model.namespace);
}
render() {
const { children, ...rest } = this.props;
return (
<Output {...rest}>
<this.Output {...rest}>
{ children }
</Output>
</this.Output>
);
}
......
......@@ -3,6 +3,7 @@ import PropTypes from 'prop-types';
import modelCreator from './model';
import connect from '../../hoc/stateful';
import TableEx from '../../../components/table/index';
import { shallowEqual } from '../../../utils/helper';
import styles from './index.less';
class DsTable extends React.Component {
......@@ -14,7 +15,7 @@ class DsTable extends React.Component {
componentWillReceiveProps(nextProps) {
const { coordinate, params } = nextProps;
const { dispatchLocal } = this.props;
if (coordinate !== this.props.coordinate || params !== this.props.params) {
if (!shallowEqual(coordinate, this.props.coordinate) || !shallowEqual(params, this.props.params)) {
dispatchLocal({ type: 'doInit', payload: { coordinate, params } });
}
}
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论