Unverified Commit 2af441cf authored by Nicholas Ramkissoon's avatar Nicholas Ramkissoon Committed by GitHub

chore(examples): Convert `with-react-hook-form` example to TypeScript (#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: 's avatarBalázs Orbán <18369201+balazsorban44@users.noreply.github.com>
parent 4cb96fb0
...@@ -7,8 +7,14 @@ ...@@ -7,8 +7,14 @@
}, },
"dependencies": { "dependencies": {
"next": "latest", "next": "latest",
"react": "^17.0.2", "react": "^18.2.0",
"react-dom": "^17.0.2", "react-dom": "^18.2.0",
"react-hook-form": "7.4.0" "react-hook-form": "^7.33.1"
},
"devDependencies": {
"@types/node": "^18.0.6",
"@types/react": "^18.0.15",
"@types/react-dom": "^18.0.6",
"typescript": "^4.7.4"
} }
} }
import { AppProps } from 'next/app'
import '../styles/global.css' import '../styles/global.css'
export default function MyApp({ Component, pageProps }) { export default function MyApp({ Component, pageProps }: AppProps) {
return <Component {...pageProps} /> return <Component {...pageProps} />
} }
import { useState } from 'react' import { useState } from 'react'
import { useForm } from 'react-hook-form' import { useForm } from 'react-hook-form'
interface User {
name: string
}
interface LoginFormValues {
username: string
password: string
remember: boolean
}
const IndexPage = () => { const IndexPage = () => {
const [user, setUser] = useState() const [user, setUser] = useState<User>()
const { const {
register, register,
formState: { errors }, formState: { errors },
handleSubmit, handleSubmit,
} = useForm() } = useForm<LoginFormValues>()
const onSubmit = ({ username, password, remember }) => { const onSubmit = handleSubmit(({ username, password, remember }) => {
// You should handle login logic with username, password and remember form data // You should handle login logic with username, password and remember form data
setUser({ name: username }) setUser({ name: username })
} })
return ( return (
<div className="container"> <div className="container">
{user ? ( {user ? (
<span className="hello-user">Hello, {user.name}!</span> <span className="hello-user">Hello, {user.name}!</span>
) : ( ) : (
<form onSubmit={handleSubmit(onSubmit)}> <form onSubmit={onSubmit}>
<div className="row"> <div className="row">
<h3 className="form-header">LOGIN</h3> <h3 className="form-header">LOGIN</h3>
</div> </div>
......
{
"compilerOptions": {
"target": "es5",
"lib": ["dom", "dom.iterable", "esnext"],
"allowJs": true,
"skipLibCheck": true,
"strict": false,
"forceConsistentCasingInFileNames": true,
"noEmit": true,
"esModuleInterop": true,
"module": "esnext",
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"jsx": "preserve",
"incremental": true
},
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx"],
"exclude": ["node_modules"]
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment