Skip to content

Commit 2af441c

Browse files
chore(examples): Convert with-react-hook-form example to TypeScript (vercel#38796)
Converted `with-react-hook-form` example to TypeScript as it is pointed out in the contributing docs that examples should be converted to TS. - updated dependencies - fixed/added types where necessary ## Documentation / Examples - [x] Make sure the linting passes by running `pnpm lint` - [x] The examples guidelines are followed from [our contributing doc](https://github.com/vercel/next.js/blob/canary/contributing.md#adding-examples) Co-authored-by: Balázs Orbán <[email protected]>
1 parent 4cb96fb commit 2af441c

File tree

5 files changed

+50
-13
lines changed

5 files changed

+50
-13
lines changed

examples/with-react-hook-form/package.json

+9-3
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,14 @@
77
},
88
"dependencies": {
99
"next": "latest",
10-
"react": "^17.0.2",
11-
"react-dom": "^17.0.2",
12-
"react-hook-form": "7.4.0"
10+
"react": "^18.2.0",
11+
"react-dom": "^18.2.0",
12+
"react-hook-form": "^7.33.1"
13+
},
14+
"devDependencies": {
15+
"@types/node": "^18.0.6",
16+
"@types/react": "^18.0.15",
17+
"@types/react-dom": "^18.0.6",
18+
"typescript": "^4.7.4"
1319
}
1420
}

examples/with-react-hook-form/pages/_app.js

-5
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { AppProps } from 'next/app'
2+
import '../styles/global.css'
3+
4+
export default function MyApp({ Component, pageProps }: AppProps) {
5+
return <Component {...pageProps} />
6+
}

examples/with-react-hook-form/pages/index.js examples/with-react-hook-form/pages/index.tsx

+15-5
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,34 @@
11
import { useState } from 'react'
22
import { useForm } from 'react-hook-form'
33

4+
interface User {
5+
name: string
6+
}
7+
8+
interface LoginFormValues {
9+
username: string
10+
password: string
11+
remember: boolean
12+
}
13+
414
const IndexPage = () => {
5-
const [user, setUser] = useState()
15+
const [user, setUser] = useState<User>()
616
const {
717
register,
818
formState: { errors },
919
handleSubmit,
10-
} = useForm()
11-
const onSubmit = ({ username, password, remember }) => {
20+
} = useForm<LoginFormValues>()
21+
const onSubmit = handleSubmit(({ username, password, remember }) => {
1222
// You should handle login logic with username, password and remember form data
1323
setUser({ name: username })
14-
}
24+
})
1525

1626
return (
1727
<div className="container">
1828
{user ? (
1929
<span className="hello-user">Hello, {user.name}!</span>
2030
) : (
21-
<form onSubmit={handleSubmit(onSubmit)}>
31+
<form onSubmit={onSubmit}>
2232
<div className="row">
2333
<h3 className="form-header">LOGIN</h3>
2434
</div>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"compilerOptions": {
3+
"target": "es5",
4+
"lib": ["dom", "dom.iterable", "esnext"],
5+
"allowJs": true,
6+
"skipLibCheck": true,
7+
"strict": false,
8+
"forceConsistentCasingInFileNames": true,
9+
"noEmit": true,
10+
"esModuleInterop": true,
11+
"module": "esnext",
12+
"moduleResolution": "node",
13+
"resolveJsonModule": true,
14+
"isolatedModules": true,
15+
"jsx": "preserve",
16+
"incremental": true
17+
},
18+
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx"],
19+
"exclude": ["node_modules"]
20+
}

0 commit comments

Comments
 (0)