我在应用程序中使用打字稿,但是发现react-redux出现了一些问题。 “连接”方法报告了一个问题,由于我是打字稿和Redux的新秀,所以我对此一无所知。我应该怎么做或应该在代码中的什么地方进行修改?非常感谢
使用[email protected]
,[email protected]
,[email protected]
构建的应用程序。
// article
interface IArticle {
title: string,
author: string
}
// state
interface MyState {
list: [],
article: IArticle
}
// stateMapToProps
interface MyStateProps {
article: IArticle
}
// router match
interface MatchParams {
id: string
}
// own props
interface MyOwnProps extends RouteComponentProps < MatchParams > {
article: IArticle,
dispatch: (o: object) => {}
}
class ArticleContainer extends Component < MyOwnProps, {} > {
constructor(props: MyOwnProps) {
super(props);
}
componentDidMount() {
const {
dispatch
} = this.props;
const id = this.props.match.params.id
dispatch(fetchArticle(id))
}
render() {
const {
article
} = this.props;
return ( <
Article article = {
article
} > < /Article>
)
}
}
const mapStateToProps = (state: MyState): MyStateProps => {
return {
article: state.article
}
}
export default connect < MyStateProps, {}, {
article: IArticle
} > (
mapStateToProps
)(ArticleContainer)
这是异步动作
fetchArticle
的代码function fetchArticle(id: string) {
return function(dispatch: (action: AnyAction) => {}): Promise<void> {
dispatch(getArticle(id))
return axios.get(`/article/${id}`)
.then(res => {
dispatch(getArticleSuccess(res.data))
})
}
}
export
行发生错误,并且消息如下:类型'(state:MyState)=> MyStateProps'的参数不可分配
参数类型为“ MapStateToPropsParam”。类型'(state:MyState)=> MyStateProps'不是
可分配给“ MapStateToPropsFactory”类型。
参数“状态”和“初始状态”的类型不兼容。
类型“ {}”缺少类型“ MyState”中的以下属性:list,articlets(2345)
最佳答案
能够编译代码的最少步骤:MyOwnProps
应该是
import { AnyAction } from 'redux';
interface AppThunkAction<TAction> {
(dispatch: (action: TAction) => void, getState: () => MyState): any;
}
// As you're going to dispatch thunk actions, dispatch should be overloaded
interface Dispatch<TAction> {
(action: AppThunkAction<TAction>): any
(action: TAction): TAction
}
// own props
interface MyOwnProps extends RouteComponentProps<MatchParams> {
article: IArticle,
dispatch: Dispatch<AnyAction>
}
如果要为
connect
函数提供类型,请添加MyState
作为最后一个类型,如下所示export default connect <MyStateProps, {}, {
article: IArticle
}, MyState >(
mapStateToProps
)(ArticleContainer)
或者您可以允许编译器自己推断类型,这是首选
export default connect(
mapStateToProps
)(ArticleContainer)
所以工作结果
import { Component } from 'react';
import { RouteComponentProps } from 'react-router-dom';
import { connect, ResolveThunks } from 'react-redux';
import { AnyAction } from 'redux';
import axios from 'axios';
// article
interface IArticle {
title: string,
author: string
}
// state
interface MyState {
list: [],
article: IArticle
}
// stateMapToProps
interface MyStateProps {
article: IArticle
}
// router match
interface MatchParams {
id: string
}
export interface AppThunkAction<TAction> {
(dispatch: (action: TAction) => void, getState: () => MyState): any;
}
interface Dispatch<TAction> {
(action: AppThunkAction<TAction>): any
(action: TAction): TAction
}
// own props
interface MyOwnProps extends RouteComponentProps<MatchParams> {
article: IArticle,
dispatch: Dispatch<AnyAction>
}
function getArticle(id: string) {
return {
type: 'GET_ARTICLE',
id
}
}
function getArticleSuccess(i: any) {
return {
type: 'SET_ARTICLE',
i
}
}
const fetchArticle = (id: string): AppThunkAction<AnyAction> =>
(dispatch, getState) => {
dispatch(getArticle(id))
return axios.get(`/article/${id}`)
.then(res => {
dispatch(getArticleSuccess(res.data))
})
}
class ArticleContainer extends Component<MyOwnProps, {}> {
constructor(props: MyOwnProps) {
super(props);
}
componentDidMount() {
const {
dispatch
} = this.props;
const id = this.props.match.params.id
dispatch(fetchArticle(id))
}
render() {
const {
article
} = this.props;
return (<div>article: {article}</div>
)
}
}
const mapStateToProps = (state: MyState): MyStateProps => {
return {
article: state.article
}
}
export default connect(
mapStateToProps
)(ArticleContainer)
关于javascript - 如何在带有Typescript的react-redux中正确使用'connect',我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/56963211/