完善类型
This commit is contained in:
parent
12fae0d238
commit
4900251b21
|
@ -103,7 +103,7 @@ 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) {
|
if (!options) {
|
||||||
options = {};
|
options = {};
|
||||||
|
@ -111,14 +111,13 @@ export function connect<StateProps, DispatchProps, OwnProps, MergedProps>(
|
||||||
|
|
||||||
//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) => {
|
||||||
const store = useStore() as ReduxStoreHandler;
|
const store = useStore() as ReduxStoreHandler;
|
||||||
const [state, setState] = useState(() => store.getState());
|
const [state, setState] = useState(() => store.getState());
|
||||||
|
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const unsubscribe = store.subscribe(() => {
|
const unsubscribe = store.subscribe(() => {
|
||||||
setState(store.getState());
|
setState(store.getState());
|
||||||
|
@ -126,13 +125,13 @@ export function connect<StateProps, DispatchProps, OwnProps, MergedProps>(
|
||||||
return () => unsubscribe();
|
return () => unsubscribe();
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
const previous = useRef<{ state: { [key: string]: any }, mappedState: StateProps }>({
|
const previous = useRef<{ state: { [key: string]: any }; mappedState: StateProps }>({
|
||||||
state: {},
|
state: {},
|
||||||
mappedState: {} as StateProps,
|
mappedState: {} as StateProps,
|
||||||
});
|
});
|
||||||
|
|
||||||
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 {
|
||||||
|
@ -169,7 +168,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