完善类型

This commit is contained in:
huangxuan 2023-11-30 17:26:32 +08:00
parent 12fae0d238
commit 4900251b21
No known key found for this signature in database
GPG Key ID: E79F50C67022565D
1 changed files with 5 additions and 6 deletions

View File

@ -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 });
}); });