第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

帶有 React Bootstrap 的 TypeScript 可重用表單

帶有 React Bootstrap 的 TypeScript 可重用表單

寶慕林4294392 2023-01-06 16:21:39
我正在關(guān)注這篇文章以學(xué)習(xí)反應(yīng)和打字稿形式。有一個(gè)可重用的表單,其中解釋了 React Bootstrap,我喜歡實(shí)現(xiàn)它。我試圖通過(guò)編寫如下的用戶表單來(lái)做到這一點(diǎn)。但是我明白了TS7031:綁定元素“取消”隱式具有“任意”類型一旦我解決了這個(gè)錯(cuò)誤,我就不知道如何使用它了。例如,如果我想實(shí)現(xiàn)一個(gè)登錄表單和一個(gè)登錄表單,我該如何參考下面的用戶表單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;}
查看完整描述

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;


查看完整回答
反對(duì) 回復(fù) 2023-01-06
  • 1 回答
  • 0 關(guān)注
  • 158 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)