gpt4 book ai didi

reactjs - 如何使用 onSubmit 输入 Form 组件?

转载 作者:行者123 更新时间:2023-12-03 13:27:05 24 4
gpt4 key购买 nike

我有自己的Form我想将其与 Parent Component 分开。

const Form: React.FC<any> = ({ children, handleFormSubmit }) => (
<form onSubmit={handleFormSubmit}>{children}</form>
);

Parent Component

const ParentComponent = () => {
...

const handleFormSubmit = (event: React.FormEvent<HTMLFormElement>) => {
event.preventDefault();
publishReview(review).then(() => setReview(initialReviewState));
};

return (
<Form onSubmit={handleFormSubmit}>
...
</Form>
)}

我以为handleFormSubmit将从其声明 const handleFormSubmit = (event: React.FormEvent<HTMLFormElement>) 获取类型.

但事实并非如此

我尝试构建一个界面,甚至使用 any类型:

interface FormProps {
handleFormSubmit: any
}
const Form: React.FC<FormProps>= ({ children, handleFormSubmit }) => (
<form onSubmit={handleFormSubmit}>{children}</form>
);

但它给出了以下错误:

const Form: React.FunctionComponent<FormProps>
Type '{ children: Element; onSubmit: (event: FormEvent<HTMLFormElement>) => void; }' is not assignable to type 'IntrinsicAttributes & FormProps & { children?: ReactNode; }'.
Property 'onSubmit' does not exist on type 'IntrinsicAttributes & FormProps & { children?: ReactNode; }'.

最佳答案

这对我有用。只是根据您的猜测进行猜测。谢谢!

handlePostForm = (e: React.FormEvent) => {

关于reactjs - 如何使用 onSubmit 输入 Form 组件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56322667/

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