【问题标题】:Wizard form in React TypeScript react-final-formReact TypeScript 中的向导表单 react-final-form
【发布时间】:2020-07-08 22:25:06
【问题描述】:

我想在 React.js 中使用带有 TypeScript 的 react-final-form 创建一个 3 步表单。我从codesandbox 中获取了模式,但我对 const static Page 有疑问。我不知道如何将它重写为 TypeScript,老实说我不知道​​它是如何工作的......

到目前为止,我得到了这个:

向导.tsx

  import React, { useState } from "react";
import { Form } from "react-final-form";

type Wizard = {
  onSubmit: (values: Values) => void;
};

type Values = {
  name: String;
  surname: String;
  email: String;
  password: String;
  city: String;
  birthDay: Number;
  birthMonth: Number;
  birthYear: Number;
};

// 3-steps form
const Wizard: React.FC<Wizard> = ({ onSubmit, children }) => {
  const [page, setPage] = useState(0);
  const [values, setValues] = useState<Values | undefined>(undefined);
  const activePage = React.Children.toArray(children)[page];
  const isLastPage = page === React.Children.count(children) - 1;

  const static Page = (children) => children;

  // next page
  const next = (values: Values) => {
    setPage(Math.min(page + 1, React.Children.count(children)));
    setValues(values);
  };

  // previous page
  const previous = () => {
    setPage(Math.max(page - 1, 0));
  };

  const handleSubmit = (values: Values) => {
    const isLastPage = page === React.Children.count(children) - 1;
    if (isLastPage) {
      return onSubmit(values);
    } else {
      next(values);
    }
  };

  return (
    <Form onSubmit={handleSubmit}>
      {({ handleSubmit, submitting, values }) => {
        <form onSubmit={handleSubmit}>
          {activePage}
          <div className="buttons">
            {page > 0 && (
              <button type="button" onClick={previous}>
                « Powrót
              </button>
            )}
            {!isLastPage && <button type="submit">Dalej »</button>}
            {isLastPage && (
              <button type="submit" disabled={submitting}>
                Zakończ
              </button>
            )}
          </div>
        </form>;
      }}
    </Form>
  );
};

export default Wizard;

注册.tsx

    import React from "react";
import Wizard from "./wizard";

import styles from "./register.module.scss";

const Register: React.FC = () => {
  const onSubmit = () => {
    console.log("onSubmit");
  };

  return (
    <Wizard onSubmit={onSubmit}>
      <Wizard.Page>Page 1</Wizard.Page>
      <Wizard.Page>Page 2</Wizard.Page>
      <Wizard.Page>Page 3</Wizard.Page>
    </Wizard>
  );
};

export default Register;

错误 静态: const static: any Variable 'static' implicitly has an 'any' type.ts(7005)

在页面上: const Page: (children: any) =&gt; any ',' expected.ts(1005)

【问题讨论】:

    标签: javascript reactjs typescript react-final-form


    【解决方案1】:

    只需将Page 拉出为单独的FC。没有充分的理由让它成为 &lt;Wizard.Page&gt; 而不是 &lt;WizardPage&gt;

    可以通过让Wizard 成为interface WizardWithPage extends React.FC 等来维持它的状态。但这是浪费精力,只需将Page 拉到`Wizard 之外。

    【讨论】:

    • 你的意思是export const WizardPage: React.FC = ({ children }) =&gt; { return children; };上面const Wizard =[...]吗?
    • 如果你这样做了,但是我在 WizardPage 上出现了这个错误,我不知道如何处理..Type '({ children }: { children?: ReactNode; }) =&gt; ReactNode' is not assignable to type 'FC&lt;{}&gt;'. Type 'ReactNode' is not assignable to type 'ReactElement&lt;any, any&gt; | null'. Type 'undefined' is not assignable to type 'ReactElement&lt;any, any&gt; | null'
    • 好的,我是这样固定的:export const WizardPage: React.FC = ({ children }) =&gt; &lt;div&gt;{children}&lt;/div&gt;;
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-29
    • 1970-01-01
    • 2022-07-05
    • 1970-01-01
    相关资源
    最近更新 更多