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 example #21245

Merged
merged 5 commits into from
May 5, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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
34 changes: 34 additions & 0 deletions examples/with-react-hook-form/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.

# dependencies
/node_modules
/.pnp
.pnp.js

# testing
/coverage

# next.js
/.next/
/out/

# production
/build

# misc
.DS_Store
*.pem

# debug
npm-debug.log*
yarn-debug.log*
yarn-error.log*

# local env files
.env.local
.env.development.local
.env.test.local
.env.production.local

# vercel
.vercel
23 changes: 23 additions & 0 deletions examples/with-react-hook-form/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# with react-hook-form

This example shows how to integrate react-hook-form in Next.js

Form handling doesn't have to be painful. React Hook Form will help you write less code while achieving better performance. For more information, see [react-hook-form](https://react-hook-form.com)

## Deploy your own

Deploy the example using [Vercel](https://vercel.com/now):

[![Deploy with Vercel](https://vercel.com/button)](https://vercel.com/new/git/external?repository-url=https://github.com/vercel/next.js/tree/canary/examples/with-react-hook-form&project-name=with-react-hook-form&repository-name=with-react-hook-form)

## How to use

Execute [`create-next-app`](https://github.com/vercel/next.js/tree/canary/packages/create-next-app) with [npm](https://docs.npmjs.com/cli/init) or [Yarn](https://yarnpkg.com/lang/en/docs/cli/create/) to bootstrap the example:

```bash
npx create-next-app --example with-react-hook-form with-react-hook-form-app
# or
yarn create next-app --example with-react-hook-form with-react-hook-form-app
```

Deploy it to the cloud with [Vercel](https://vercel.com/new?utm_source=github&utm_medium=readme&utm_campaign=next-example) ([Documentation](https://nextjs.org/docs/deployment)).
57 changes: 57 additions & 0 deletions examples/with-react-hook-form/components/Login.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { useForm } from 'react-hook-form'

const Login = () => {
const { register, errors, handleSubmit } = useForm()
const login = handleSubmit(({ username, password, remember }) => {
console.log(
`username:${username}, password:${password}, remember:${remember}`
timneutkens marked this conversation as resolved.
Show resolved Hide resolved
)
// Handle login logic with username, password and remember form data
aydinkn marked this conversation as resolved.
Show resolved Hide resolved
})

return (
<form onSubmit={login}>
<div>
<input
type="text"
name="username"
placeholder="user name"
ref={register({
required: { value: true, message: 'User name is required' },
minLength: {
value: 3,
message: 'User name cannot be less than 3 character',
},
})}
className={'form-field' + (errors.username ? ' has-error' : '')}
/>
{errors.username && (
<span className="error">{errors.username.message}</span>
)}
</div>
<div>
<input
type="password"
name="password"
placeholder="password"
ref={register({
required: { value: true, message: 'Please enter your password' },
})}
className={'form-field' + (errors.password ? ' has-error' : '')}
/>
{errors.password && (
<span className="error">{errors.password.message}</span>
)}
</div>
<div>
<input type="checkbox" name="remember" id="remember" ref={register} />
<label htmlFor="remember">Remember me</label>
</div>
<div>
<button type="submit">Login</button>
</div>
</form>
)
}

export default Login
16 changes: 16 additions & 0 deletions examples/with-react-hook-form/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "with-react-hook-form",
"version": "1.0.0",
"scripts": {
"dev": "next dev",
"build": "next build",
"start": "next start"
},
"dependencies": {
"next": "latest",
"react": "17.0.1",
"react-dom": "17.0.1",
"react-hook-form": "^6.14.1"
},
"license": "MIT"
}
5 changes: 5 additions & 0 deletions examples/with-react-hook-form/pages/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import Login from '../components/Login'

const IndexPage = () => <Login />
aydinkn marked this conversation as resolved.
Show resolved Hide resolved

export default IndexPage