npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-ssg-netlify-forms

v1.0.3

Published

A turn-key wrapper to seamlessly enable Netlify Forms in React-based SSGs (Next, Gatsby, etc..)

Downloads

25

Readme

React SSG Netlify Form

A drop-in all-in-one solution for getting Netlify Forms on React-based SSGs (Gatsby, Next, etc..) including automatic honeypot protection 💯📝

Demo: https://react-ssg-netlify-forms.demo.jon.fm/

Premise

The <NetlifyForm> is a turn-key solution to enable your Gatsby, Next, etc. site with Netlify forms without having to worry about all of the details. Manage your form's state however you prefer (a simple method shown below) then pass the value collection to the component and let it handle the rest.

No need to configure anything in the Netlify UI, no need to worry about fetching or endpoints. It's all included.

Example

Simple form usage (https://react-ssg-netlify-forms.demo.jon.fm)

import React, { useState } from "react"
import { navigate } from 'gatsby'
import NetlifyForm from 'react-ssg-netlify-forms'

const IndexPage = () => {

  // Post-Submit Navigate
  const postSubmit = () => {
    navigate('/hooray')
  }

  // Simple controlled form setup (Control your own state)
  const handleChange = e => setFormValues({ ...formValues, [e.target.name]: e.target.value })
  const [formValues, setFormValues] = useState({
    name: '',
    message: ''
  })

  return (
      <NetlifyForm formName="Very Simple Form" formValues={formValues} postSubmit={postSubmit} >
        <div>
          Your Name: <input type="text" name="name" value={formValues.name} onChange={handleChange} required />
        </div>
        <div>
          Message: <textarea name="message" value={formValues.message} onChange={handleChange} required />
        </div>
        <div>
          <button type="submit">Send</button>
        </div>
      </NetlifyForm>
  )
}

Install

Install is straightforward as can be. Install via Yarn or NPM:

Yarn: yarn add react-ssg-netlify-forms

NPM: npm i react-ssg-netlify-forms

Then import into your React code where desired (shown above): import NetlifyForm from 'react-ssg-netlify-forms'

Usage

The <NetlifyForm> component operates on the following premise and allows for a number of configuration options.

The Component will handle all of the plumbing and leg work to make sure that your form data arrives to Netlify safe and sound, but it's up to you to manage the state of your form controls. The Component expects you to pass an object in for formValues with a flat structure, ready to be serialized. For instance:

{
  name: 'Hello there, friend!',
  email: '[email protected]',
  description: 'What a beautiful day it is',
  favoriteColor: 'red'
}

This is the natural progression if using the example above where form state is managed in a single object (which also makes having a generic onChange handler possible 😁). The key reason for this is so that you can employ your own validations and other UX flow concerns with the form data at your fingertips. This is better illustrated in a slightly more complicated example.

formName - a required prop - You may make this whatever you'd like - this is what will show up in the Netlify UI as the 'name' label for your form. This name is also what's referenced in the Subject field of Forms notifications emails.

formValues - a required prop - This is the object that holds the form data / the values of the fields at time of submit event; described above

preSubmit - an optional prop - A function that the <NetlifyForm> component will call when the user kicks off the submit event, before the content is sent to Netlify. This function must return true for the submit to proceed, as returning false (or something non-truthy) results in a short-circuit no-op for the form. This may be an Async function. It will be awaited before submission, but still must return something truthy. This function will not receive arguments.

A note: preSubmit is where you'd want to manifest any sort of form validations or checks you'd like to run. Please see this example for an idea of what that might look like

postSubmit - an optional prop - A function that the <NetlifyForm> component will call after submitting the form data to Netlify. This may be an Async function. This function will not receive arguments.


Why?

Well, the short of it is that Netlify Forms is awesome but can be a tad tricky for the React-based SSGs. This package aims to take as much complexity away as possible. It generates the right code at build time then augments it differently at runtime and handles the submission process to Netlify servers for you.

Tl;dr: it makes life easier 🙂