commit
75bed609fe
|
@ -76,9 +76,10 @@ export function isSame(x: unknown, y: unknown): boolean {
|
||||||
if (typeof x !== typeof y) {
|
if (typeof x !== typeof y) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
// 如果两个对象都是null或undefined,直接返回true
|
// 此时x和y类型一致,若都为undefined或null返回true,但也有可能此时为一个对象和null,这种情况直接比较
|
||||||
|
// typeof null === 'object'
|
||||||
if (x == null || y == null) {
|
if (x == null || y == null) {
|
||||||
return true;
|
return x === y;
|
||||||
}
|
}
|
||||||
// 如果两个对象都是基本类型,比较他们的值是否相等
|
// 如果两个对象都是基本类型,比较他们的值是否相等
|
||||||
if (typeof x !== 'object') {
|
if (typeof x !== 'object') {
|
||||||
|
|
|
@ -103,15 +103,11 @@ export function connect<StateProps, DispatchProps, OwnProps, MergedProps>(
|
||||||
dispatchProps,
|
dispatchProps,
|
||||||
ownProps
|
ownProps
|
||||||
): MergedProps => ({ ...stateProps, ...dispatchProps, ...ownProps } as unknown as MergedProps),
|
): MergedProps => ({ ...stateProps, ...dispatchProps, ...ownProps } as unknown as MergedProps),
|
||||||
options?: ConnectOption
|
options: ConnectOption = {},
|
||||||
): Connector<OwnProps, MergedProps> {
|
): Connector<OwnProps, MergedProps> {
|
||||||
if (!options) {
|
|
||||||
options = {};
|
|
||||||
}
|
|
||||||
|
|
||||||
//this component should bear the type returned from mapping functions
|
//this component should bear the type returned from mapping functions
|
||||||
return (Component: OriginalComponent<MergedProps>): WrappedComponent<OwnProps> => {
|
return (Component: OriginalComponent<MergedProps>): WrappedComponent<OwnProps> => {
|
||||||
const useStore = createStoreHook(options?.context || DefaultContext);
|
const useStore = createStoreHook(options.context || DefaultContext);
|
||||||
|
|
||||||
//this component should mimic original type of component used
|
//this component should mimic original type of component used
|
||||||
const Wrapper: WrappedComponent<OwnProps> = (props: OwnProps) => {
|
const Wrapper: WrappedComponent<OwnProps> = (props: OwnProps) => {
|
||||||
|
@ -131,7 +127,7 @@ export function connect<StateProps, DispatchProps, OwnProps, MergedProps>(
|
||||||
});
|
});
|
||||||
|
|
||||||
let mappedState: StateProps;
|
let mappedState: StateProps;
|
||||||
if (options?.areStatesEqual) {
|
if (options.areStatesEqual) {
|
||||||
if (options.areStatesEqual(previous.current.state, state)) {
|
if (options.areStatesEqual(previous.current.state, state)) {
|
||||||
mappedState = previous.current.mappedState as StateProps;
|
mappedState = previous.current.mappedState as StateProps;
|
||||||
} else {
|
} else {
|
||||||
|
@ -168,7 +164,7 @@ export function connect<StateProps, DispatchProps, OwnProps, MergedProps>(
|
||||||
return createElement(Component, mergedProps);
|
return createElement(Component, mergedProps);
|
||||||
};
|
};
|
||||||
|
|
||||||
if (options?.forwardRef) {
|
if (options.forwardRef) {
|
||||||
const forwarded = forwardRef((props, ref) => {
|
const forwarded = forwardRef((props, ref) => {
|
||||||
return Wrapper({ ...props, ref: ref });
|
return Wrapper({ ...props, ref: ref });
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in New Issue