javascript - 如何在流程中输入 react-redux connect?

标签 javascript reactjs react-redux flowtype

这就是我的 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);

这会产生以下错误,我不知道这意味着什么或如何解决它。

[flow] Cannot call connect because property currentProduct is missing in React.StatelessFunctionalComponent [1] in the indexer property's key of type argument ST. (References: [1])

最佳答案

建议您将 Flow 至少升级到 0.89 以上,并使用 Flow Typed's newest React Redux library definition .

然后,您可以使用PropsOwnProps 注释连接的组件>

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)

Here是一份更详细的指南,其中包含更多用例。

关于javascript - 如何在流程中输入 react-redux connect?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53397055/

相关文章:

javascript - react Redux reducer : 'this.props.tasks.map is not a function' error

javascript - 有没有办法从 "getElementsByClassName(' 数据中提取 ID')”?

javascript - 为什么Javascript的每个方法都被压倒了?

javascript - 如何让 2 个文本框根据下拉菜单选择更改值?

javascript - 当我盯着元素时,jQuery ID 选择器返回空白数组

javascript - 状态未初始化为 reducer 的初始状态

javascript - 如何从 defaultProps 迁移到默认参数

reactjs - 使用 nextJS 在路由更改时自行重置 Redux 存储

reactjs - 如何在渲染之外从 React Context Consumer 获取数据

reactjs - 如何将 redux 与 antd 表单验证集成