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

react-hook-form v6 to v7 migration #4

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"react": "17.0.1",
"react-dom": "17.0.1",
"react-firebase-hooks": "^2.2.0",
"react-hook-form": "^6.14.2",
"react-hook-form": "^7.3.6",
"react-hot-toast": "^1.0.2",
"react-markdown": "^5.0.3"
}
Expand Down
19 changes: 10 additions & 9 deletions pages/admin/[slug].js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { useState } from 'react';
import { useRouter } from 'next/router';

import { useDocumentDataOnce } from 'react-firebase-hooks/firestore';
import { useForm } from 'react-hook-form';
import { useForm, useFormState } from 'react-hook-form';
import ReactMarkdown from 'react-markdown';
import Link from 'next/link';
import toast from 'react-hot-toast';
Expand Down Expand Up @@ -55,9 +55,11 @@ function PostManager() {
}

function PostForm({ defaultValues, postRef, preview }) {
const { register, errors, handleSubmit, formState, reset, watch } = useForm({ defaultValues, mode: 'onChange' });
const { register, handleSubmit, reset, watch, formState: { errors }, control } = useForm({ defaultValues, mode: 'onChange' });

const { isValid, isDirty } = formState;
const { isValid, isDirty } = useFormState({
control
});

const updatePost = async ({ content, published }) => {
await postRef.update({
Expand All @@ -83,18 +85,17 @@ function PostForm({ defaultValues, postRef, preview }) {
<ImageUploader />

<textarea
name="content"
ref={register({
{...register("content", {
maxLength: { value: 20000, message: 'content is too long' },
minLength: { value: 10, message: 'content is too short' },
required: { value: true, message: 'content is required' },
})}
minLength: { value: 10, message: 'content is too short' },
required: { value: true, message: 'content is required' },
})}
></textarea>

{errors.content && <p className="text-danger">{errors.content.message}</p>}

<fieldset>
<input className={styles.checkbox} name="published" type="checkbox" ref={register} />
<input className={styles.checkbox} type="checkbox" {...register("published")} />
<label>Published</label>
</fieldset>

Expand Down