如何解决带React Bootstrap的TypeScript可重用表单
我正在追踪this post,以学习反应和打字稿形式。 React Bootstrap有一个可重用的形式,我喜欢实现它。我试图通过编写如下用户表格来做到这一点。但是我得到了
TS7031:绑定元素“取消”隐式具有“任意”类型
解决这个错误后,我不知道如何使用它。例如,如果我想实现登录表单和登录表单,该如何参考下面的用户表单
import React from 'react';
import Form from 'react-bootstrap/Form';
import Button from 'react-bootstrap/Button';
import styled from 'styled-components';
const UserForm =
({
cancel,errors,submit,submitButtonText,elements,passwordErrors
}) => {
function handleSubmit(event) {
event.preventDefault();
submit();
}
function handleCancel(event) {
event.preventDefault();
cancel();
}
return (
<React.Fragment>
<ErrorsDisplay errors={errors} passwordErrors={passwordErrors} />
<Form onSubmit={handleSubmit}>
{elements()}
<Button className="mr-1" variant="primary" type="submit">{submitButtonText}</Button>
<Button className="mr-1" variant="secondary" onClick={handleCancel}>Cancel</Button>
</Form>
</React.Fragment>
);
};
function ErrorsDisplay({ errors,passwordErrors}) {
let errorDisplay = null;
if (errors.length) {
errorDisplay = (
<React.Fragment>
<ValidiationLabel>Errors:</ValidiationLabel>
<ValidiationUl>
{errors.map((error,i) => (
<li key={i}>{error}</li>
))}
</ValidiationUl>
</React.Fragment>
);
} else if (!passwordErrors) {
errorDisplay = (
<React.Fragment>
<ValidiationLabel>Errors:</ValidiationLabel>
<ValidiationUl>{<li>Passwords must match</li>}</ValidiationUl>
</React.Fragment>
);
}
return errorDisplay;
}
const ValidiationUl = styled.div`
color: red;
padding: 15px 0 40px 10px;
`;
const ValidiationLabel = styled.h2`
color: #0069c0;
font-size: 28px;
`;
export default UserForm;
解决方法
首先,假设您使用的是打字稿项目(.tsx文件扩展名),则需要输入UserForm的参数:
// 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) => { .....
和用于ErrorsDisplay功能:
interface ErrorsProps {
errors: string[];
passwordErrors: boolean;
}
function ErrorsDisplay({ errors,passwordErrors }: ErrorsProps) {...
对于句柄功能,您需要指定事件的类型:
// 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>) { ....
完成此操作后,您可以在任何地方使用UserForm,例如登录页面/登录页面。
您只需要导入它即可:
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;
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。