gpt4 book ai didi

reactjs - 将 Redux-Form 与 React-Redux connect 结合使用时出现 TypeScript 错误

转载 作者:搜寻专家 更新时间:2023-10-30 20:39:03 24 4
gpt4 key购买 nike

我爱上了 TypeScript,直到我发现 Redux-Form 与 React-Redux 之间存在一些非常令人沮丧的不兼容性。

我的目标是用 react-redux connect 装饰器包装一个 reduxForm 装饰组件——这种模式在 babel 配置中一直对我有用,并且似乎遵循 HOC 方法.这是一个例子:

import * as React from 'react';
import { Dispatch } from 'redux';
import { connect } from 'react-redux';
import { reduxForm, Field, InjectedFormProps } from 'redux-form';

interface SampleFormData {
username: string;
}

interface SampleFormProps {
saveData: (data: SampleFormData) => void;
}

type AllSampleFormProps = SampleFormProps & InjectedFormProps<SampleFormData>;

const SampleForm: React.SFC<AllSampleFormProps> = (props) => (
<form onSubmit={props.handleSubmit(props.saveData)}>
<Field name="username" component="input" />
</form>
);

const DecoratedSampleForm = reduxForm<SampleFormData>({ form: "sampleForm" })(SampleForm);

export default connect(
() => ({}),
(dispatch) => ({
saveData: (data: SampleFormData) => dispatch({ type: "SAVE_DATA", data })
})
)(DecoratedSampleForm);

这是 TypeScript 抛出的错误:

> Argument of type 'DecoratedComponentClass<SampleFormData,
> Partial<ConfigProps<SampleFormData, {}>>>' is not assignable to
> parameter of type 'ComponentType<{ saveData: (data: SampleFormData) =>
> { type: string; data: SampleFormData; }; }>'.
>
> Type 'DecoratedComponentClass<SampleFormData,
> Partial<ConfigProps<SampleFormData, {}>>>' is not assignable to type
> 'StatelessComponent<{ saveData: (data: SampleFormData) => { type:
> string; data: SampleFormData; };...'.
>
> Type 'DecoratedComponentClass<SampleFormData,
> Partial<ConfigProps<SampleFormData, {}>>>' provides no match for the
> signature '(props: { saveData: (data: SampleFormData) => { type:
> string; data: SampleFormData; }; } & { children?: ReactNode; },
> context?: any): ReactElement<any>'.

有没有人找到让 react-redux 接受 DecoratedComponentClass 类型的解决方案?我找到了一个 suggestion 来使用“中间”组件,但我还没有设法让它与 thunk Action 一起使用。另外,我发现在输入表单的 Prop 方面,这会产生比解决问题更多的问题。

最佳答案

对于遇到此问题的任何人,我发现我可以通过为连接语句提供空的 TStatePropsTDispatchProps 对象来消除错误。

interface SampleFormData {
username: string;
}

interface SampleFormProps {
saveData: (data: SampleFormData) => void;
}

type AllSampleFormProps = SampleFormProps & InjectedFormProps<SampleFormData>;

const SampleForm: React.SFC<AllSampleFormProps> = (props) => (
<form onSubmit={props.handleSubmit(props.saveData)}>
<Field name="username" component="input" />
</form>
);

const DecoratedSampleForm = reduxForm<SampleFormData>({ form: "sampleForm" })(SampleForm);

export default connect<{},{}>(
() => ({}),
(dispatch) => ({
saveData: (data: SampleFormData) => dispatch({ type: "SAVE_DATA", data })
})
)(DecoratedSampleForm);

这样做的一个缺点是它迫使我们盲目地提供连接 Prop ,但我觉得这是一个比编写重写 @types 声明更优雅的解决方案。

为了解决这个缺点,我能够通过提供正确的接口(interface)而不是空对象来验证类型;但是,此方法只能临时执行以检查绑定(bind),因为它不能解决 DecoratedComponentClass 错误。

export default connect<{}, SampleFormProps, InjectedFormProps<SampleFormData>>(
() => ({}),
(dispatch) => ({
saveData: (data: SampleFormData) => dispatch({ type: "SAVE_DATA", data })
})
)(DecoratedSampleForm);

关于reactjs - 将 Redux-Form 与 React-Redux connect 结合使用时出现 TypeScript 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46477939/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com