提交 1bc9bab4 authored 作者: vipcxj's avatar vipcxj

improve size hoc

上级 300c4d01
import React from 'react';
import PropTypes from 'prop-types';
import { shallowEqual } from '../../utils/helper';
class CheckSize extends React.Component {
shouldComponentUpdate(nextProps, nextState, nextContext) {
return !shallowEqual(this.props.size, nextProps.size)
|| !shallowEqual(this.props, nextProps, ['size', 'children'])
|| !shallowEqual(this.state, nextState)
|| !shallowEqual(this.context, nextContext);
}
render() {
return this.props.children || null;
}
}
CheckSize.propTypes = {
size: PropTypes.object,
};
export default CheckSize;
...@@ -2,7 +2,6 @@ import React from 'react'; ...@@ -2,7 +2,6 @@ import React from 'react';
import sm from 'react-sizeme'; import sm from 'react-sizeme';
import hoistStatics from 'hoist-non-react-statics'; import hoistStatics from 'hoist-non-react-statics';
import { shallowEqual } from '../../utils/helper'; import { shallowEqual } from '../../utils/helper';
import CheckSize from './CheckSize';
const SizeMeContext = React.createContext('sizeMe'); const SizeMeContext = React.createContext('sizeMe');
export const { Provider, Consumer } = SizeMeContext; export const { Provider, Consumer } = SizeMeContext;
...@@ -38,9 +37,7 @@ export const withSize = (Component) => { ...@@ -38,9 +37,7 @@ export const withSize = (Component) => {
const C = React.forwardRef((props, ref) => ( const C = React.forwardRef((props, ref) => (
<Consumer> <Consumer>
{ size => ( { size => (
<CheckSize {...props} size={size} fordwardedRef={ref}>
<Component {...props} size={size} ref={ref} /> <Component {...props} size={size} ref={ref} />
</CheckSize>
)} )}
</Consumer> </Consumer>
)); ));
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论