打字稿和Redux连接

时间:2018-11-20 19:06:18

标签: reactjs typescript redux react-redux

我正在尝试使用Typescript将redux连接到组件,并继续遇到相同的错误。

  

“ typeof BaseLayoutUnconnected”类型的参数不可分配给   类型为'Component '的参数。属性“ setState”为   类型'typeof BaseLayoutUnconnected'中缺少。

import * as React from 'react';
import { IBaseLayoutProps, IBaseLayoutState } from './base-layout.types';
import { ChatContainer } from '../../components';
import { connect, DispatchProp } from 'react-redux';
import { bindActionCreators, Dispatch } from 'redux';
import { RouteComponentProps } from 'react-router';
import { ChatActions } from 'app/actions';
import { RootState } from 'app/reducers';
import { omit } from 'app/utils';

export const mapStateToProps = (state: RootState, ownProps) => {
    return {
        chatItems: state.chatItems
    };
};

export const mapDispatchToProps = (dispatch: Dispatch) => ({
    actions: bindActionCreators(omit(ChatActions, 'Type'), dispatch)
});

export class BaseLayoutUnconnected extends React.Component<IBaseLayoutProps, IBaseLayoutState> {
   constructor(props) {
        super(props);
        this.state = {};
    }

    render() {
        const { actions, chatItems } = this.props;
        return <ChatContainer actions={actions} chatItems={chatItems} />;
    }
}

export const BaseLayout = connect(
    mapStateToProps,
    mapDispatchToProps
)(BaseLayoutUnconnected);

这是通过以下方式在我的app.tsx中调用的:

 <Route exact={true} path="/" component={BaseLayout} />

这里是道具和状态

export interface IBaseLayoutProps {
    chatItems: RootState.ChatState;
    actions: ChatActions;
}

export interface IBaseLayoutState {}

ChatActions外观

import { createAction } from 'redux-actions';
import { ChatItemModel } from 'app/models';

export namespace ChatActions {
    export enum Type {
        ADD_CHAT_ITEM = 'ADD_CHAT_ITEM'
    }

    export const addChatItem = createAction<PartialPick<ChatItemModel, 'text'>>(Type.ADD_CHAT_ITEM);
}

export type ChatActions = Omit<typeof ChatActions, 'Type'>;

1 个答案:

答案 0 :(得分:3)

这也是我初次使用Redux和TypeScript时遇到的问题。有一个棘手的解决方案。 connect方法具有许多泛型。我尝试用您的例子来解释它。

首先,您必须拆分BaseLayoutUnconnected的属性。

export interface IBaseLayoutStateProps {
    chatItems: RootState.ChatState;
}

export interface IBaseLayoutDispatchProps {
    actions: ChatActions;
}

export interface IBaseLayoutOwnProps {
    // put properties here you want to make available from the connected component
}

export type IBaseLayoutProps = IBaseLayoutOwnProps & IBaseLayoutDispatchProps & IBaseLayoutStateProps 

export interface IBaseLayoutState {}

然后,您必须填充不同redux函数的泛型。

const mapStateToProps: MapStateToProps<IBaseLayoutStateProps, {}, RootState> = (state: RootState): IBaseLayoutStateProps => ({
    chatItems: state.chatItems
})

export const mapDispatchToProps: MapDispatchToPropsFunction<IBaseLayoutDispatchProps, IBaseLayoutOwnProps> = (dispatch: Dispatch, ownProps: IBaseLayoutDispatchProps): IBaseLayoutDispatchProps => ({
    actions: bindActionCreators(omit(ChatActions, 'Type'), dispatch)
});

export default connect<IBaseLayoutStateProps , IBaseLayoutDispatchProps, IBaseLayoutOwnProps , RootState>(
    mapStateToProps,
    mapDispatchToProps
)(BaseLayoutUnconnected as any)

一个很好的资源,在这里您可以找到我写的所有这些东西,还有this repository