提交 300c4d01 authored 作者: vipcxj's avatar vipcxj

improve size hoc

上级 de701f2f
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;
import React from 'react';
import PropTypes from 'prop-types';
import sm from 'react-sizeme';
import hoistStatics from 'hoist-non-react-statics';
import { shallowEqual } from '../../utils/helper';
import CheckSize from './CheckSize';
const SizeMeContext = React.createContext('sizeMe');
export const { Provider, Consumer } = SizeMeContext;
export const provideSize = options => (Component) => {
const WrappedComponent = ({ size, forwardedRef, children, ...rest }) => {
const WC = class WrappedComponent extends React.Component {
render() {
const { size, forwardedRef, children, ...rest } = this.props;
if (!shallowEqual(this.size, size)) {
this.size = size;
}
return (
<Provider value={size}>
<Provider value={this.size}>
<Component ref={forwardedRef} {...rest}>
{children || null}
</Component>
</Provider>
);
}
};
const SizeMeComponent = sm(options)(WrappedComponent);
const SizeMeComponent = sm(options)(WC);
const C = React.forwardRef((props, ref) => (
<SizeMeComponent forwardedRef={ref} {...props} />
));
......@@ -28,22 +34,6 @@ export const provideSize = options => (Component) => {
export const provideWidth = provideSize({});
export const provideHeight = provideSize({ monitorHeight: true });
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 const withSize = (Component) => {
const C = React.forwardRef((props, ref) => (
<Consumer>
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论