kir*_*rpt 5 typescript reactjs redux-form
我正在尝试将自定义道具传递给我的装饰组件reduxForm.我也是新手稿.
第一个问题是我不能用connect连接装饰组件:
export default
connect(mapStateToProps)(
reduxForm({
form: 'myform'
})(MyComponent)
)
Run Code Online (Sandbox Code Playgroud)
错误:
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'.
Run Code Online (Sandbox Code Playgroud)
这显然是由错误的类型引起的,但正如我所说,我是Typescript的新手,我不知道如何处理这些长错误.此时我不需要将任何道具传递给validate表单函数,但它对将来的任务非常有帮助.
主要问题是我无法将自定义道具传递给装饰组件:
export default reduxForm({
form: 'myform'
})(
connect(mapStateToProps)(MyComponent)
);
Run Code Online (Sandbox Code Playgroud)
表格道具:
interface Props extends InjectedFormProps {
onSubmit: () => void;
// some values from the state
loading: boolean; // the custom prop
}
Run Code Online (Sandbox Code Playgroud)
当我尝试这个:
<MyForm loading onSubmit={this.handleSubmit} />
Run Code Online (Sandbox Code Playgroud)
它引发了另一个错误:
Error:(134, 25) TS2339:Property 'loading' does not exist on type 'IntrinsicAttributes & IntrinsicClassAttributes<FormInstance<any, Partial<ConfigProps<any, {}>>>> ...'.
Run Code Online (Sandbox Code Playgroud)
奇怪的是我能够传递在InjectedFormProps界面中声明的道具,但我无法传递任何自定义道具.实际上,我可以从mapStateToProps函数中传递任何道具.也许我只是无法将任何自定义道具传递给装饰组件reduxForm.
小智 10
@types/redux-form7.0.10和redux-form7.1.2的工作解决方案:
定义表单组件MyForm.tsx:
import * as React from "react";
import { InjectedFormProps, reduxForm } from 'redux-form';
import { connect } from 'react-redux';
interface StateProps {
valueFromState: string;
}
interface Props extends StateProps {
loading: boolean; // the custom prop
}
const MyForm: React.StatelessComponent<Props & InjectedFormProps<{}, Props>> =
({handleSubmit, loading}) => (
<form onSubmit={handleSubmit}>
{loading && (<p>loading...</p>)}
</form>
)
const mapStateToProps = (state: any): StateProps => ({valueFromState: "1"});
export default connect(mapStateToProps)(
reduxForm<{}, Props>({
form: 'myform'
})(MyForm)
);
Run Code Online (Sandbox Code Playgroud)
然后使用它:
import MyForm from './MyForm';
<MyForm onSubmit={() => console.log("submit")} loading />
Run Code Online (Sandbox Code Playgroud)
这里有一个如何定义自定义道具的示例:
import { FormProps, reduxForm } from "redux-form";
interface InitialValuesProps {
name: string;
}
interface CustomFormProps extends FormProps<InitialValuesProps, {}, {}> {
loading: boolean;
}
class CustomForm extends React.Component<CustomFormProps> {
render() {
const loading = this.props.loading
const name = this.props.initialValues.name;
}
}
export default reduxForm({ form: 'myForm' })(CustomForm)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3574 次 |
| 最近记录: |