|
| 1 | +<center> |
| 2 | + |
| 3 | +<h1 style="display:none;">react-netlify-forms</h1> |
| 4 | + |
| 5 | +> 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. |
| 6 | +
|
| 7 | +[](https://www.npmjs.com/package/react-netlify-forms) [](https://standardjs.com) |
| 8 | + |
| 9 | +<div style="display:none;" aria-hidden> |
| 10 | + |
| 11 | +[Go to documentation with live demo.](https://pyrax.github.io/react-netlify-forms) |
| 12 | + |
| 13 | +</div> |
| 14 | + |
| 15 | +</center> |
| 16 | + |
| 17 | +## Features |
| 18 | + |
| 19 | +It gives you all the features of Netlify Forms as simple and **reusable** React components which have been tested on Netlify. |
| 20 | + |
| 21 | +- Default form handlers with support for file uploads. |
| 22 | +- Spam prevention through included honeypot and reCAPTCHA components. |
| 23 | +- Can be used alone or together with form libraries such as Formik or react-hook-form. |
| 24 | + |
| 25 | +<Alert variant='muted'> |
| 26 | + |
| 27 | +## Before using |
| 28 | + |
| 29 | +This component must be used with Server-Side Rendering (SSR) because Netlify |
| 30 | +searches for a `data-netlify` attribute on HTML `form` tags to setup their |
| 31 | +Forms backend for you. |
| 32 | + |
| 33 | +➡️ _Suggestions for SSR: GatsbyJS, Next.js, react-static_ |
| 34 | + |
| 35 | +</Alert> |
| 36 | + |
| 37 | +## Install |
| 38 | + |
| 39 | +Either install with NPM via: |
| 40 | + |
| 41 | +```bash |
| 42 | +npm install --save react-netlify-forms |
| 43 | +``` |
| 44 | + |
| 45 | +or with YARN via: |
| 46 | + |
| 47 | +```bash |
| 48 | +yarn add react-netlify-forms |
| 49 | +``` |
| 50 | + |
| 51 | +## Usage |
| 52 | + |
| 53 | +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: |
| 54 | + |
| 55 | +```jsx |
| 56 | +import React, { Component } from 'react' |
| 57 | + |
| 58 | +import { NetlifyForm, Honeypot } from 'react-netlify-forms' |
| 59 | + |
| 60 | +export default ContactForm = () => ( |
| 61 | + <NetlifyForm name='Contact' action='/thanks' honeypotName='bot-field'> |
| 62 | + {({ handleChange, success, error }) => ( |
| 63 | + <> |
| 64 | + <Honeypot /> |
| 65 | + {success && <p>Thanks for contacting us!</p>} |
| 66 | + {error && ( |
| 67 | + <p>Sorry, we could not reach our servers. Please try again later.</p> |
| 68 | + )} |
| 69 | + <div> |
| 70 | + <label htmlFor='name'>Name:</label> |
| 71 | + <input type='text' name='name' id='name' onChange={handleChange} /> |
| 72 | + </div> |
| 73 | + <div> |
| 74 | + <label htmlFor='message'>Message:</label> |
| 75 | + <textarea |
| 76 | + type='text' |
| 77 | + name='message' |
| 78 | + id='message' |
| 79 | + rows='4' |
| 80 | + onChange={handleChange} |
| 81 | + /> |
| 82 | + </div> |
| 83 | + <div> |
| 84 | + <button type='submit'>Submit</button> |
| 85 | + </div> |
| 86 | + </> |
| 87 | + )} |
| 88 | + </NetlifyForm> |
| 89 | +) |
| 90 | +``` |
| 91 | + |
| 92 | +For more examples please browse through our website. |
| 93 | + |
| 94 | +## License |
| 95 | + |
| 96 | +MIT © [Björn Clees](https://github.com/Pyrax) |
| 97 | + |
| 98 | +--- |
| 99 | + |
| 100 | +Thanks to [create-react-library](https://www.npmjs.com/package/create-react-library) for providing quick setup for NPM packages. |
0 commit comments