javascript - 如何输入 react-redux connect in flow?
问题描述
这就是我的mapStateToProps
样子。
const mapStateToProps = (state): StateProps => {
let status = false;
if (state.productsPage.currentProduct) {
if (state.productsPage.currentProduct.status === "ACTIVE") {
status = true;
}
}
return {
showModal: state.productsPage.showModal,
currentProduct: state.productsPage.currentProduct,
isLoading: state.status.loading.PRODUCTS_EDIT,
initialValues: {
name: state.productsPage.currentProduct ? state.productsPage.currentProduct.name : "",
status,
},
};
};
这里是形状StateProps
type StateProps = {
showModal: boolean,
currentProduct: Object,
isLoading: boolean,
initialValues: {
name: string,
status: boolean,
}
}
这是我对连接的使用。
const connected = connect<React$StatelessFunctionalComponent<any>, StateProps>(mapStateToProps, mapDispatchToProps);
这会产生以下错误,我不知道这意味着什么或如何解决它。
[流程] 无法调用
connect
,因为索引器属性的参数类型键中的 [1] 中currentProduct
缺少属性。(参考文献:[1])React.StatelessFunctionalComponent
ST
解决方案
建议您将 Flow 至少升级到 0.89 以上,并使用Flow Typed 最新的 React Redux 库定义。
Props
然后,您可以使用和注释连接的组件OwnProps
import * as React from 'react'
type OwnProps = {|
passthrough: string,
forMapStateToProps: string
|}
type Props = {|
...OwnProps,
fromMapStateToProps: string,
dispatch1: () => void
|}
const MyComponent = (props: Props) => (
<div onClick={props.dispatch1}>
{props.passthrough}
{props.fromMapStateToProps}
</div>
)
export default connect<Props, OwnProps, _, _, _, _>(
mapStateToProps,
mapDispatchToProps
)(MyComponent)
这是一个更详细的指南,其中包括更多用例。
推荐阅读
- python - 我的标题在我的 txt 文件的第一列。我想创建一个 Pandas DF
- python - 查找最近编辑的文件的 Python 脚本(从前两天到现在编辑)
- image - Matlab中的图像处理低通滤波器
- node.js - 如何通过托管在 IIS 服务器上在 https 上运行节点应用程序
- laravel - Laravel - 搜索提交未重定向到下一个刀片
- python - Python:使用循环提取多列的日期时间值并忽略 NaN
- swiftui - swiftui+combine:为什么滚动 LazyVGrid 时 isFavoriteO 改变了?
- python - 如何创建字典并将多列的值存储到一列并将其重建回原始数据框?
- javascript - 为什么这个 JavaScript 没有执行?
- python - 如何在模块级别的 pytest 脚本中定义要在夹具中使用的值?