gpt4 book ai didi

javascript - 如何使用 Typescript 在 react-redux 中正确使用 'connect'

转载 作者:行者123 更新时间:2023-11-30 19:25:24 25 4
gpt4 key购买 nike

我在应用程序中使用 typescript,但发现 react-redux 存在一些问题。 'connect' 方法报告了一个问题,我不知道它,因为我是 typescript 和 redux 的新手。我应该做什么或应该修改我的代码中的什么地方?非常感谢

使用 typescript@3.3react@16.8.5react-redux@7.1.0 构建的应用程序。

// 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行,信息如下:

Argument of type '(state: MyState) => MyStateProps' is not assignable to parameter of type 'MapStateToPropsParam'. Type '(state: MyState) => MyStateProps' is not assignable to type 'MapStateToPropsFactory'. Types of parameters 'state' and 'initialState' are incompatible. Type '{}' is missing the following properties from type 'MyState': list, articlets(2345)

最佳答案

能够编译代码的最少步骤:

  1. 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>
    }
  2. 如果你想为 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com