Проверка формы хуков React с помощью Material-UI

У меня есть проект Next.js с Material-UI в качестве UI-фреймворка.

Я выполняю валидацию с помощью формы хуков React.

Мой основной компонент имеет форму, и у меня есть дочерние компоненты для разных полей ввода.

Мой компонент формы показан ниже:

import { Paper } from "@material-ui/core";
import React from "react";
import { makeStyles, withStyles } from "@material-ui/core/styles";
import CPTextBox from "../../components/Form/CPTextBox";
import { useForm } from "react-hook-form";

const useStyles = makeStyles((theme) => ({
  root: {
    flexGrow: 1,
    "& > *": {
      margin: theme.spacing(1),
    },
  },
  paper: {
    padding: theme.spacing(2),
    textAlign: "center",
    color: theme.palette.text.secondary,
  },
}));

function create2() {
  const classes = useStyles();
  const { control, register, handleSubmit } = useForm();
  const onSubmit = (data) => {
    console.log(data);
  };

  return (
    <Paper className={classes.paper}>
      <form onSubmit={handleSubmit(onSubmit)}>
        <CPTextBox
          attributes={{
            name: "question",
            id: "text_box_1",
            label: "Enter the Question",
            disabled: false,
            type: "",
            fullWidth: true,
            control: { control },
          }}
        />
        <input type="submit" />
      </form>
    </Paper>
  );
}

export default create;

Компонент CPTextBox выглядит так:


import React, { useState } from "react";
import InputLabel from "@material-ui/core/InputLabel";
import FormControl from "@material-ui/core/FormControl";
import { makeStyles, useTheme } from "@material-ui/core/styles";
import { Grid, TextField } from "@material-ui/core";
import { Controller } from "react-hook-form";

const useStyles = makeStyles((theme) => ({
  formControl: {
    // margin: theme.spacing(1),
    minWidth: 120,
    fullWidth: true,
  },
  textbox: {
    width: "100%",
  },
}));

export default function CPTextBox(props) {
  const classes = useStyles();
  return (
    <FormControl fullWidth variant="outlined" className={classes.formControl}>

      <Controller
        as={TextField}
        name={props.attributes.name}
        control={props.attributes.control}
        defaultValue=""
      />
    </FormControl>
  );
}

Используя этот код, он всегда выдает ошибку, как показано ниже:

Ошибка сервера
TypeError: невозможно прочитать свойство isReValidateOnBlur из undefined

Как исправить ошибку?


person Asish Antony    schedule 20.01.2021    source источник


Ответы (2)


Атрибут control в атрибутах не должен быть объектом. Это должен быть сам элемент управления:

attributes={{
            name: "question",
            id: "text_box_1",
            label: "Enter the Question",
            disabled: false,
            type: "",
            fullWidth: true,
            control: control,
          }}
person Thanh    schedule 20.01.2021

Проблема заключалась в переносе объекта управления внутри атрибутов:

<CPTextBox
          attributes={{
            name: "question",
            id: "text_box_1",
            label: "Enter the Question",
            disabled: false,
            type: "",
            fullWidth: true,
            **control: control ,**
          }}
        />
person Asish Antony    schedule 20.01.2021