Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Aula - React Forms #4

Open
EdiJunior88 opened this issue Apr 4, 2023 · 0 comments
Open

Aula - React Forms #4

EdiJunior88 opened this issue Apr 4, 2023 · 0 comments

Comments

@EdiJunior88
Copy link

EdiJunior88 commented Apr 4, 2023

Reparei que as versões do pacote YUP e REACT-HOOK-FORM utilizadas no vídeo estão defasadas e instalando as novas versões consegui ajustar o código do AddTodo.tsx e está funcionando:

package.json

 "@hookform/resolvers": "^3.0.0",
"react-hook-form": "^7.43.9",
 "yup": "^1.0.2"

código

import React, { useContext } from "react";
import { TodoContext } from "../contexts/TodoContext";
import { useForm } from "react-hook-form";
import { yupResolver } from "@hookform/resolvers/yup";
import * as yup from "yup";
import { TodoContextType } from "../contexts/TodoContextType";

type AddTodoForm = {
  title: string;
};

const schema = yup
  .object()
  .shape({
    title: yup.string().required("Tarefa inválida"),
  })
  .required();

const AddTodo = () => {
  const { addTodo } = useContext<TodoContextType>(TodoContext);
  const {
    register,
    handleSubmit,
    formState: { errors },
  } = useForm<AddTodoForm>({
    resolver: yupResolver(schema),
  });

  const onSubmit = (data: AddTodoForm, e: any) => {
    addTodo(data.title);
    e.target.reset();
    window.location.href = "/";
  };

  return (
    <form onSubmit={handleSubmit(onSubmit)}>
      <h4>Nova tarefa</h4>
      <div className='uk-margin uk-width-1-1'>
        <input
          type='text'
          id='title'
          placeholder='Nova tarefa...'
          className='uk-input'
          {...register("title")}
        />
        <span>
          <small>
            <strong className='uk-text-danger'>{errors.title?.message}</strong>
          </small>
        </span>
      </div>
      <div className='uk-width-1-1'>
        <button type='submit' className='uk-button uk-button-primary'>
          Salvar
        </button>
      </div>
    </form>
  );
};

export default AddTodo;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant