【问题标题】:How to use material Ui in react with typescript如何使用材料 Ui 与打字稿反应
【发布时间】:2020-12-20 22:50:14
【问题描述】:

这是我的代码,它工作正常,但我想用它来使用材质 ui。但是,例如,当我使用 TextField 而不是输入时,我得到了错误

Uncaught (in promise) Error: Request failed with status code 422
    at createError (createError.js:16)
    at settle (settle.js:17)
    at XMLHttpRequest.handleLoad (xhr.js:69)

我安装了材料ui的npm包,然后导入TextField,然后用它代替input..

所以我不明白这里有什么问题?有人可以帮忙吗?

import * as React from "react";
import { useState } from "react";
import { useForm } from "react-hook-form";
import TextField from "@material-ui/core/TextField";

import axios from "axios";

import "./App.css";

type FormData = {
  first: string;
  infix: string;
  last: string;
  email: string;
  message: string;
};
function App() {
  const { register, handleSubmit } = useForm<FormData>();
  const [first, setVoor] = useState("");
  const [infix, setTussen] = useState("");
  const [last, setAchter] = useState("");
  const [email, setEmail] = useState("");
  const [message, setMessage] = useState("");

  const onSubmit = handleSubmit(({ first, infix, last, email }) => {
    setVoor(first);
    setTussen(infix);
    setAchter(last);
    setEmail(email);

    axios
      .post(" https://code-application-api.devheld.nl/registration", {
        first,
        infix,
        last,
        email,
      })
      .then((res) => {
        console.log(res.data, "data?");
        setMessage(res.data.message);
      });
  });

  return (
    <form onSubmit={onSubmit}>
     <TextField name="first" ref={register} label="voornaam" />
      <label style={{ color: "black" }}>Tussenvoegsel</label>
      <input name="infix" ref={register} />
      <label style={{ color: "black" }}>Achternaam</label>
      <input name="last" ref={register} />
      <label style={{ color: "black" }}>E-mail</label>
      <input name="email" ref={register} />

      <input type="submit" />

      <p>{first}</p>
      <p>{infix}</p>
      <p>{last}</p>
      <p>{email}</p>
      {message && <p>{message}</p>}
    </form>
    // <p>{Object.keys(email)}</p>
  );
}

export default App;

【问题讨论】:

标签: reactjs typescript material-ui react-hooks


【解决方案1】:

您需要注意一些事情并进行一些配置。 Material-UI Documentation

【讨论】:

  • 这种回复最好作为评论而不是作为答案。见stackoverflow.com/help/how-to-answer。您可以通过添加细节来改善这一点,向提问者解释如何解决他们的问题。链接到文档不是答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-02-05
  • 2020-11-30
  • 2021-10-18
  • 1970-01-01
  • 2021-12-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多