我有一个 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/