【发布时间】:2021-02-09 05:14:20
【问题描述】:
有一个 HOC 组件用于存储所有元素的一个状态的值作为输入,选择。输出函数接受参数({text: Component, select: Component})。输入参数时,错误显示为
TS2322: Type '({ settings }: Param) => (type: any) => any' is not assignable to type 'FC<Param>'. Type '(type: any) => any' is missing the following properties from type 'ReactElement<any, any>': type, props, key
app.js
const useSelectComponent = CustomFabric({
text: Input, // component view
select: Select, // component view
});
function App() {
const InputField = useSelectComponent('text');
const SelectField = useSelectComponent('select');
return (
<form>
<InputField/>
<SelectField/>
</form>
);
}
在接口中,我表示对象元素将包含一个函数,第二个接口描述了一个带有实体的对象。如何正确指定 HOC 函数的类型,以便我接受带有组件的对象
特设:
interface PropsTypes {
text: () => void;
select: () => void;
}
interface Param {
settings: PropsTypes;
}
export const CustomFabric: React.FC<Param> = ({ settings }: Param) => {
const elements = Object.entries(settings);
const newSettings = elements.reduce((acc: any, item: any) => {
const key = item[0];
const Component = item[1];
acc[key] = (props: any) => {
const [value, setValue] = useState('');
const onChange = (event: { target: { value: any } }) => {
setValue(event.target.value);
};
return <Component value={value} onChange={onChange} {...props} />;
};
return acc;
}, {});
return (type: any) => {
return newSettings[type];
};
};
【问题讨论】:
-
函数组件是一个函数,它接受一个参数——一个 props 对象——并返回一些 JSX 元素。您的
CustomFabric接受道具,但它返回一个函数。所以它本质上与React.FC类型不兼容。 -
我正试图弄清楚这段代码应该做什么,因为类型与您使用它的方式不匹配。看起来
text和select应该是组件,但是您的界面说它们是不带参数且不返回任何内容的函数。 -
我也根本不“明白”在状态位于无法访问的黑盒子后面的受控输入的意义。
CustomFabric似乎无法让您访问该州。但它可能更适合作为钩子而不是函数组件。它绝对不是一个函数组件。 -
@LindaPaiste感谢您的帮助,这对我有用,如果您可以添加作为问题的答案,我会接受。
标签: reactjs typescript typescript-typings