完善类型

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,
ownProps
): MergedProps => ({ ...stateProps, ...dispatchProps, ...ownProps } as unknown as MergedProps),
options: ConnectOption,
options?: ConnectOption
): Connector<OwnProps, MergedProps> {
if (!options) {
options = {};
@ -111,14 +111,13 @@ export function connect<StateProps, DispatchProps, OwnProps, MergedProps>(
//this component should bear the type returned from mapping functions
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
const Wrapper: WrappedComponent<OwnProps> = (props: OwnProps) => {
const store = useStore() as ReduxStoreHandler;
const [state, setState] = useState(() => store.getState());
useEffect(() => {
const unsubscribe = store.subscribe(() => {
setState(store.getState());
@ -126,13 +125,13 @@ export function connect<StateProps, DispatchProps, OwnProps, MergedProps>(
return () => unsubscribe();
}, []);
const previous = useRef<{ state: { [key: string]: any }, mappedState: StateProps }>({
const previous = useRef<{ state: { [key: string]: any }; mappedState: StateProps }>({
state: {},
mappedState: {} as StateProps,
});
let mappedState: StateProps;
if (options.areStatesEqual) {
if (options?.areStatesEqual) {
if (options.areStatesEqual(previous.current.state, state)) {
mappedState = previous.current.mappedState as StateProps;
} else {
@ -169,7 +168,7 @@ export function connect<StateProps, DispatchProps, OwnProps, MergedProps>(
return createElement(Component, mergedProps);
};
if (options.forwardRef) {
if (options?.forwardRef) {
const forwarded = forwardRef((props, ref) => {
return Wrapper({ ...props, ref: ref });
});