javascript - React 元素中的 React Redux Flowtype "property not found"从 redux connect 获取属性

标签 javascript reactjs redux react-redux flowtype

我有一个 React 组件,它使用 react-redux connect 函数(装饰器)来设置属性。在 JSX 中引用此组件时,flowtype 会提示“在...React 元素的 props 中找不到属性

type SidebarCmpProps = {
  activeSidebarComponent: string,
  actions: { openCallMember: () => void }
}

@connect(
  (state) => {
    return { activeSidebarComponent: state.sidebar.activeSidebarComponent }
  },
  (dispatch) => ({ actions: bindActionCreators({ openCallMember }, dispatch) })
)
class Sidebar extends React.Component {
  props: SidebarCmpProps
  static propTypes = {
    actions: React.PropTypes.object.isRequired,
    activeSidebarComponent: React.PropTypes.string
  }
}

准确的错误是:

 65:         
             ^^^^^^^^^^^ React element `Sidebar`
 56:   props: SidebarCmpProps
              ^^^^^^^^^^^^^^^ property `actions`. Property not found in...
 65:         
             ^^^^^^^^^^^ props of React element `Sidebar`

To get around the error I had to change the properties to union types of any and add a defaultProps, which is less than ideal

type SidebarCmpProps = {
  activeSidebarComponent: any | string,
  actions: any | { openCallMember: () => void }
}
class Sidebar extends React.Component {
  static defaultProps: SidebarCmpProps = {
    actions: null,
    activeSidebarComponent: null
  }
}

有没有更好的解决方案?

最佳答案

那么,您将获得 属性“ Action ”。 Property not found in 错误,因为您只是在 @connect 中设置 activeSidebarComponent prop,并且将操作声明为必需的 prop actions: React.PropTypes.object .isRequired.

因此,您必须为 actions 属性设置默认值或删除该限制。

关于javascript - React 元素中的 React Redux Flowtype "property not found"从 redux connect 获取属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41384030/

相关文章:

javascript - React Redux Router 不路由

Javascript 仅从表中的第一行选取值

在 DOM 中替换的 Javascript

javascript - 尝试使用AJAX获取函数参数并传递给PHP代码

javascript - 如何获取失败的ajax错误消息

javascript - 在 Meteor React 客户端中显示所有用户

javascript - useEffect 缺少依赖项,但是当我添加它时,出现 'maximum update depth exceeded' 错误

javascript - 当提供查询参数时,React 查询字符串显示 404

javascript - 从 reducer 访问 Redux 存储值,无需控制操作创建者

javascript - React redux,操作有效负载无法在 try/catch block 中看到变量