栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

在打字稿中使用react-redux connect

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

在打字稿中使用react-redux connect

我注意到几件事:

1)据我在示例中看到的以及

props
在Typescript中使用时,您对的调用
React.Component
需要
Props
像这样指定类型参数:

export default class TestPage extends React.Component<Test.Props, Test.State>{    constructor(props: Test.Props) {        super(props);    }}

您可以指定您的组件不接受

props
state
通过传递空接口,即:

export default class TestPage extends React.Component<{}, {}>{    // constructor becomes 'useless' at this point and is not needed    constructor() {        super();    }}

我认为这可以解释您的呼叫签名不匹配的原因,以及为什么看不见任何属性

this.props
-TS看到一个接口,
ReadOnly{}
因为它没有传入类型参数。

2)您的

mapStateToProps
功能看起来不太正确。
mapStateToProps
接受两个参数
state
((引用您的Redux
store
)和
ownProps
作为可选的第二个参数,该参数引用
props
从父级传递过来的参数)。因此
mapStateToProps
应如下所示:

function mapStateToProps(state: any, ownProps: { label: string }) {    return {        label: ownProps.label    };}

这是我为什么

connect
会引发错误的猜测-
在这里,您只是断言Redux应该如何处理
props
来自
store
props
来自父代的合并。让我知道是否可行。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/403944.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号