Skip to content

Commit e845dda

Browse files
KyleAMathewsDSchau
andauthored
feat(docs): Add sample form/function code to the function docs (#31453)
* feat(docs): Add sample form/function code to the function docs Get people going quick if they want to try adding a form that submits to a function. * Update docs/docs/how-to/functions/getting-started.md Co-authored-by: Dustin Schau <[email protected]> * Add simple server validation * Run prettier Co-authored-by: Dustin Schau <[email protected]>
1 parent d86cd9f commit e845dda

File tree

1 file changed

+69
-1
lines changed

1 file changed

+69
-1
lines changed

docs/docs/how-to/functions/getting-started.md

Lines changed: 69 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,4 +123,72 @@ export default async function postNewPersonHandler(req, res) {
123123

124124
## Forms
125125

126-
Forms and Functions are often used together. See the [basic form example](https://github.com/gatsbyjs/gatsby-functions-beta/tree/main/examples/basic-form) as well as the [Forms doc page](/docs/how-to/adding-common-features/adding-forms/) which walks you through building a custom form.
126+
Forms and Functions are often used together. For a working example you can play with locally, see the [form example](https://github.com/gatsbyjs/gatsby-functions-beta/tree/main/examples/basic-form). The [Forms doc page](/docs/how-to/adding-common-features/adding-forms/) is a gentle introduction for building forms in React. Below is sample code for a very simple form that submits to a function that you can use as a basis for building out forms in Gatsby.
127+
128+
```js:title=src/api/form.js
129+
export default function formHandler(req, res) {
130+
// req.body has the form values
131+
console.log(req.body)
132+
133+
// Here is where you would validate the form values and
134+
// do any other actions with it you need (e.g. save it somewhere or
135+
// trigger an action for the user).
136+
//
137+
// e.g.
138+
139+
if (!req.body.name) {
140+
return res.status(422).json("Name field is required")
141+
}
142+
143+
return res.json(`OK`)
144+
}
145+
```
146+
147+
```js:title=src/pages/form.js
148+
import * as React from "react"
149+
150+
export default function FormPage() {
151+
const [value, setValue] = React.useState({})
152+
const [serverResponse, setServerResponse] = React.useState(``)
153+
154+
// Listen to form changes and save them.
155+
function handleChange(e) {
156+
value[e.target.id] = e.target.value
157+
setServerResponse(``)
158+
setValue({ ...value })
159+
}
160+
161+
// When the form is submitted, send the form values
162+
// to our function for processing.
163+
async function onSubmit(e) {
164+
e.preventDefault()
165+
const response = await window
166+
.fetch(`/api/form`, {
167+
method: `POST`,
168+
headers: {
169+
"content-type": "application/json",
170+
},
171+
body: JSON.stringify(value),
172+
})
173+
.then(res => res.json())
174+
175+
setServerResponse(response)
176+
}
177+
178+
return (
179+
<div>
180+
<div>Server response: {serverResponse}</div>
181+
<form onSubmit={onSubmit} method="POST" action="/api/form">
182+
<label htmlFor="name">Name:</label>
183+
<input
184+
type="text"
185+
id="name"
186+
value={value[`name`] || ``}
187+
onChange={handleChange}
188+
/>
189+
<input type="submit" />
190+
</form>
191+
</div>
192+
)
193+
}
194+
```

0 commit comments

Comments
 (0)