我正在尝试将自定义道具传递给我用 reduxForm
装饰的组件 . 我也是新手稿 .
第一个问题是我不能用connect连接装饰组件:
export default
connect(mapStateToProps)(
reduxForm({
form: 'myform'
})(MyComponent)
)
错误:
Error:(89, 5) TS2345:Argument of type 'DecoratedComponentClass<any, Partial<ConfigProps<any, {}>>>' is not assignable to parameter of type 'ComponentType<{ addressValue: any; isDeliveryAddress: any; customerTypeValue: any; } & DispatchPr...'.
Type 'DecoratedComponentClass<any, Partial<ConfigProps<any, {}>>>' is not assignable to type 'StatelessComponent<{ addressValue: any; isDeliveryAddress: any; customerTypeValue: any; } & Dispa...'.
Type 'DecoratedComponentClass<any, Partial<ConfigProps<any, {}>>>' provides no match for the signature '(props: { addressValue: any; isDeliveryAddress: any; customerTypeValue: any; } & DispatchProp<any> & { children?: ReactNode; }, context?: any): ReactElement<any> | null'.
它需要将任何道具传递给 validate
表单函数,但它对将来的任务非常有帮助 .
主要问题是我无法将自定义道具传递给装饰组件:
export default reduxForm({
form: 'myform'
})(
connect(mapStateToProps)(MyComponent)
);
表格道具:
interface Props extends InjectedFormProps {
onSubmit: () => void;
// some values from the state
loading: boolean; // the custom prop
}
当我尝试这个:
<MyForm loading onSubmit={this.handleSubmit} />
它引发了另一个错误:
Error:(134, 25) TS2339:Property 'loading' does not exist on type 'IntrinsicAttributes & IntrinsicClassAttributes<FormInstance<any, Partial<ConfigProps<any, {}>>>> ...'.
奇怪的是我能够传递 InjectedFormProps
界面中声明的道具,但我无法传递任何自定义道具 . 实际上,我能够从 mapStateToProps
函数传递任何道具 . 也许我只是无法使用 reduxForm
将任何自定义道具传递给装饰组件 .
2 回答
@types/redux-form
7.0.10和redux-form
7.1.2的工作解决方案:在
MyForm.tsx
中定义表单组件:然后使用它:
这里有一个如何定义自定义道具的示例: