Unverified Commit abbe3b0a authored by Henrik Wenz's avatar Henrik Wenz Committed by GitHub

[Docs] Update next-forms example (#40284)

## Changelog

see commits

## 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)
parent b8d91329
{ {
"root": true,
"extends": "next/core-web-vitals" "extends": "next/core-web-vitals"
} }
/** @type {import('next').NextConfig} */
module.exports = { module.exports = {
reactStrictMode: true, reactStrictMode: true,
} }
...@@ -8,11 +8,14 @@ ...@@ -8,11 +8,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"
}, },
"devDependencies": { "devDependencies": {
"eslint": "8.4.1", "@types/node": "18.7.15",
"eslint-config-next": "latest" "@types/react": "18.0.18",
"eslint": "8.23.0",
"eslint-config-next": "latest",
"typescript": "4.8.2"
} }
} }
import type { AppProps } from 'next/app'
import '../styles/globals.css' import '../styles/globals.css'
function MyApp({ Component, pageProps }) { export default function MyApp({ Component, pageProps }: AppProps) {
return <Component {...pageProps} /> return <Component {...pageProps} />
} }
export default MyApp
export default function handler(req, res) { import type { NextApiRequest, NextApiResponse } from 'next'
type ResponseData = {
data: string
}
export default function handler(
req: NextApiRequest,
res: NextApiResponse<ResponseData>
) {
const body = req.body const body = req.body
console.log('body: ', body) console.log('body: ', body)
......
...@@ -3,7 +3,7 @@ import Image from 'next/image' ...@@ -3,7 +3,7 @@ import Image from 'next/image'
import Link from 'next/link' import Link from 'next/link'
import styles from '../styles/Home.module.css' import styles from '../styles/Home.module.css'
export default function Home() { export default function IndexPage() {
return ( return (
<div className={styles.container}> <div className={styles.container}>
<Head> <Head>
......
import Link from 'next/link' import Link from 'next/link'
import { FormEvent } from 'react'
import styles from '../styles/Home.module.css' import styles from '../styles/Home.module.css'
export default function PageWithJSbasedForm() { export default function PageWithJSbasedForm() {
// Handle the submit event on form submit. // Handle the submit event on form submit.
const handleSubmit = async (event) => { const handleSubmit = async (event: FormEvent) => {
// Stop the form from submitting and refreshing the page. // Stop the form from submitting and refreshing the page.
event.preventDefault() event.preventDefault()
// Cast the event target to an html form
const form = event.target as HTMLFormElement
// Get data from the form. // Get data from the form.
const data = { const data = {
first: event.target.first.value, first: form.first.value as string,
last: event.target.last.value, last: form.last.value as string,
} }
const JSONdata = JSON.stringify(data)
// Send the form data to our API and get a response. // Send the form data to our API and get a response.
const response = await fetch('/api/form', { const response = await fetch('/api/form', {
// Body of the request is the JSON data we created above. // Body of the request is the JSON data we created above.
body: JSONdata, body: JSON.stringify(data),
// Tell the server we're sending JSON. // Tell the server we're sending JSON.
headers: { headers: {
'Content-Type': 'application/json', 'Content-Type': 'application/json',
...@@ -53,7 +54,6 @@ export default function PageWithJSbasedForm() { ...@@ -53,7 +54,6 @@ export default function PageWithJSbasedForm() {
<input type="text" id="first" name="first" required /> <input type="text" id="first" name="first" required />
<label htmlFor="last">Last Name</label> <label htmlFor="last">Last Name</label>
<input type="text" id="last" name="last" required /> <input type="text" id="last" name="last" required />
<button type="submit">Submit</button> <button type="submit">Submit</button>
</form> </form>
</div> </div>
......
...@@ -16,16 +16,18 @@ export default function Form() { ...@@ -16,16 +16,18 @@ export default function Form() {
<code className={styles.code}>pages/no-js-form.js</code> <code className={styles.code}>pages/no-js-form.js</code>
</p> </p>
{/*action: The action attribute defines where the data gets sent. Its value must be a valid relative or absolute URL. If this attribute isn't provided, the data will be sent to the URL of the page containing the form — the current page. {/*
method: The HTTP method to submit the form with. (case insensitive) s*/} * action: The action attribute defines where the data gets sent.
* Its value must be a valid relative or absolute URL.
* If this attribute isn't provided, the data will be sent to the URL
* of the page containing the form — the current page.
* method: The HTTP method to submit the form with. (case insensitive)
*/}
<form action="/api/form" method="post"> <form action="/api/form" method="post">
<label htmlFor="first">First Name</label> <label htmlFor="first">First Name</label>
<input type="text" id="first" name="first" required /> <input type="text" id="first" name="first" required />
<label htmlFor="last">Last Name</label> <label htmlFor="last">Last Name</label>
<input type="text" id="last" name="last" required /> <input type="text" id="last" name="last" required />
<button type="submit">Submit</button> <button type="submit">Submit</button>
</form> </form>
</div> </div>
......
{
"compilerOptions": {
"target": "es5",
"lib": ["dom", "dom.iterable", "esnext"],
"allowJs": true,
"skipLibCheck": true,
"strict": true,
"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