1 回答

TA貢獻(xiàn)2019條經(jīng)驗(yàn) 獲得超9個(gè)贊
首先,假設(shè)您正在使用您所說(shuō)的 typescript 項(xiàng)目(.tsx 文件擴(kuò)展名),您需要為 UserForm 輸入?yún)?shù):
// This is just an example, not necessary needs to be this exactly types for the parameters
interface UserFormProps {
cancel(): void; // cancel is a function that returns nothing
submit(): void;
errors: string[]; // errors its an array of strings
passwordErrors: boolean;
submitButtonText: string;
elements(): ReactNode; // elements its a funtion that returns react nodes
}
// Here you are destructuring the object parameter of UserForm function.
// You telling it that its an UserFormProps Type Object on ": UserFormProps"
const UserForm = ({
cancel,
submit,
elements,
errors,
submitButtonText,
passwordErrors,
}: UserFormProps) => { .....
對(duì)于 ErrorsDisplay 函數(shù):
interface ErrorsProps {
errors: string[];
passwordErrors: boolean;
}
function ErrorsDisplay({ errors, passwordErrors }: ErrorsProps) {...
對(duì)于句柄函數(shù),您需要指定事件類型:
// You are saying the handleSubmit function receives an FormEvent from a HTML Form Element
function handleSubmit(event: React.FormEvent<HTMLFormElement>) { ....
// You are saying the handleCancel function receives an MouseEvent from a HTML Button Element
function handleCancel(event: React.MouseEvent<HTMLButtonElement>) { ....
完成此操作后,您可以在任何地方使用您的用戶窗體,例如您的登錄頁(yè)面/登錄頁(yè)面。
你只需要導(dǎo)入它:
import React from "react";
import Form from "react-bootstrap/Form";
// here you need to inform the path according to your project
import UserForm from "./UserForms";
const SignIn = () => {
return (
<UserForm
// I'm setting the values hardcoded just as example
cancel={() => {console.log('cancel')}}
submit={() => {console.log('submit')}}
errors={[]}
passwordErrors={false}
submitButtonText="test"
elements={() => (
<>
<Form.Group controlId="ControlId">
<Form.Control
type="email"
name="email"
value={"email@c.com.br"}
placeholder={"email"}
></Form.Control>
<Form.Control
type="password"
name="password"
value={"password"}
placeholder={"password"}
></Form.Control>
</Form.Group>
</>
)}
></UserForm>
);
};
export default SignIn;
添加回答
舉報(bào)