Javascript 如何在流程中键入react redux connect?
这就是我的Javascript 如何在流程中键入react redux connect?,javascript,reactjs,react-redux,flowtype,Javascript,Reactjs,React Redux,Flowtype,这就是我的MapStateTrops的外观 const mapStateToProps = (state): StateProps => { let status = false; if (state.productsPage.currentProduct) { if (state.productsPage.currentProduct.status === "ACTIVE") { status = true; }
MapStateTrops
的外观
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,
}
}
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)
这是我对connect的用法
const connected = connect<React$StatelessFunctionalComponent<any>, StateProps>(mapStateToProps, mapDispatchToProps);
const connected=connect(mapStateToProps、mapDispatchToProps);
这会产生以下错误,我不知道这意味着什么,也不知道如何着手解决它
[flow]无法调用connect
,因为属性currentProduct
无效
索引器中的React.StatelessFunctionalComponent
[1]中缺少
属性的键的类型为参数ST
。(参考文献:[1])
React$StatelessFunctionalComponent
是一个需要prop类型的泛型。而不是
您可能需要连接,而不是提供元素类型。建议您将流升级到至少超过0.89,然后使用
然后,您可以使用Props
和OwnProps
type StateProps = {
showModal: boolean,
currentProduct: Object,
isLoading: boolean,
initialValues: {
name: string,
status: boolean,
}
}
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)
import*作为来自“React”的React
类型OwnProps={|
passthrough:string,
FormPStateToprops:字符串
|}
类型道具={|
…拥有道具,
FromMapStateTrops:string,
dispatch1:()=>无效
|}
常量MyComponent=(道具:道具)=>(
{props.passthrough}
{props.fromMapStateToProps}
)
导出默认连接(
MapStateTops,
mapDispatchToProps
)(MyComponent)
是一个更详细的指南,包含了更多的用例。不确定任何vs-prop类型,您可能很快就会知道,但是connect
泛型中的第一个参数必须有一个可调用的签名,至少在我使用的react-redux flow-typed版本中是这样。