完善类型
This commit is contained in:
parent
12fae0d238
commit
4900251b21
|
@ -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 });
|
||||
});
|
||||
|
|
Loading…
Reference in New Issue