|
1 | 1 | --- |
2 | | -title: Welcome to React Netlify Forms |
| 2 | +title: react-netlify-forms |
3 | 3 | description: Get started with react-netlify-forms |
4 | 4 | --- |
5 | 5 |
|
6 | | -import Readme from '../../../README.md' |
| 6 | +import { Aside, Tabs, TabItem } from '@astrojs/starlight/components' |
7 | 7 |
|
8 | 8 | <center> |
9 | | - <img |
10 | | - src='favicon.png' |
11 | | - alt='react-netlify-forms Logo' |
12 | | - style={{ padding: '1rem 0.5rem 0rem 0.5rem', maxHeight: '120px' }} |
13 | | - /> |
| 9 | + |
| 10 | +<img src="favicon.png" alt="react-netlify-forms" style={{ maxHeight: '120px' }} /> |
| 11 | + |
| 12 | +React components and hooks giving you the power of Netlify Forms. Start building serverless forms on Netlify with React. Honeypot fields and reCAPTCHA are included as ready-to-use components. |
| 13 | + |
| 14 | +[](https://www.npmjs.com/package/react-netlify-forms) |
| 15 | +[](https://www.npmjs.com/package/react-netlify-forms) |
| 16 | +[](https://standardjs.com) |
| 17 | +[](https://github.com/Pyrax/react-netlify-forms/blob/master/LICENSE.md) |
| 18 | + |
14 | 19 | </center> |
15 | 20 |
|
16 | | -<Readme /> |
| 21 | +## Features |
| 22 | + |
| 23 | +It gives you all the features of Netlify Forms as simple and **reusable** React components which have been tested on Netlify. |
| 24 | + |
| 25 | +- Default form handlers with support for file uploads. |
| 26 | +- Spam prevention through included honeypot and reCAPTCHA components. |
| 27 | +- Can be used alone or together with form libraries such as Formik or react-hook-form. |
| 28 | + |
| 29 | +<Aside type="caution" title="Before using"> |
| 30 | + |
| 31 | +This component must be used with Server-Side Rendering (SSR) because Netlify |
| 32 | +searches for a `data-netlify` attribute on HTML `form` tags to setup their |
| 33 | +Forms backend for you. |
| 34 | + |
| 35 | +_Suggestions: Astro, Next.js, Remix or Vite_ |
| 36 | + |
| 37 | +</Aside> |
| 38 | + |
| 39 | +## Install |
| 40 | + |
| 41 | +<Tabs> |
| 42 | + <TabItem label="npm" icon="seti:npm"> |
| 43 | + ```sh |
| 44 | + npm install react-netlify-forms |
| 45 | + ``` |
| 46 | + </TabItem> |
| 47 | + <TabItem label="pnpm" icon="pnpm"> |
| 48 | + ```sh |
| 49 | + pnpm add react-netlify-forms |
| 50 | + ``` |
| 51 | + </TabItem> |
| 52 | + <TabItem label="Yarn" icon="seti:yarn"> |
| 53 | + ```sh |
| 54 | + yarn add react-netlify-forms |
| 55 | + ``` |
| 56 | + </TabItem> |
| 57 | + <TabItem label="bun" icon="bun"> |
| 58 | + ```sh |
| 59 | + bun add react-netlify-forms |
| 60 | + ``` |
| 61 | + </TabItem> |
| 62 | +</Tabs> |
| 63 | + |
| 64 | +## Usage |
| 65 | + |
| 66 | +In the following example a contact form with two inputs and a honeypot for extra spam prevention is shown. It also works without JavaScript by falling back to a serverside-rendered form which just submits data with an usual POST request: |
| 67 | + |
| 68 | +```jsx |
| 69 | +import React from 'react' |
| 70 | +import { NetlifyForm, Honeypot } from 'react-netlify-forms' |
| 71 | + |
| 72 | +const ContactForm = () => ( |
| 73 | + <NetlifyForm name='Contact' action='/thanks' honeypotName='bot-field'> |
| 74 | + {({ handleChange, success, error }) => ( |
| 75 | + <> |
| 76 | + <Honeypot /> |
| 77 | + {success && <p>Thanks for contacting us!</p>} |
| 78 | + {error && ( |
| 79 | + <p>Sorry, we could not reach our servers. Please try again later.</p> |
| 80 | + )} |
| 81 | + <div> |
| 82 | + <label htmlFor='name'>Name:</label> |
| 83 | + <input type='text' name='name' id='name' onChange={handleChange} /> |
| 84 | + </div> |
| 85 | + <div> |
| 86 | + <label htmlFor='message'>Message:</label> |
| 87 | + <textarea |
| 88 | + type='text' |
| 89 | + name='message' |
| 90 | + id='message' |
| 91 | + rows='4' |
| 92 | + onChange={handleChange} |
| 93 | + /> |
| 94 | + </div> |
| 95 | + <div> |
| 96 | + <button type='submit'>Submit</button> |
| 97 | + </div> |
| 98 | + </> |
| 99 | + )} |
| 100 | + </NetlifyForm> |
| 101 | +) |
| 102 | + |
| 103 | +export default ContactForm |
| 104 | +``` |
0 commit comments