Home
Softono

Use Formspark

Open source MIT TypeScript
17
Stars
5
Forks
0
Issues
1
Watchers
4 months
Last Commit

 About Use Formspark

React hooks for Formspark

Platforms

Web Self-hosted

Languages

TypeScript

Need Help Installing Use Formspark?

We provide expert installation service for this software. Our team will install, configure, and secure Use Formspark on your server. plans start at just $30.

Use Formspark

View on GitHub

Formspark logo

use-formspark

React hooks for Formspark.

Works with React and React Native.

Deploy

Installation

# NPM
npm install @formspark/use-formspark

# Yarn
yarn add @formspark/use-formspark

Usage

import React, { useState } from "react";
import { useFormspark } from "@formspark/use-formspark";

const ContactForm = () => {
  const [submit, submitting] = useFormspark({
    formId: "your-form-id"
  });
  const [message, setMessage] = useState("");
  return (
    <form onSubmit={async (e) => {
        e.preventDefault();
        await submit({ message });
    }}>
      <textarea
        value={message}
        onChange={(e) => setMessage(e.target.value)}
      />
      <button type="submit" disabled={submitting}>Send</button>
    </form>
  );
};

Note: don't confuse the form's action URL (e.g. https://submit-form.com/capybara) with the form ID (e.g. capybara) — this package only uses the latter.

Error handling

submit throws a FormsparkError when the server responds with a non-2xx status. The error carries the HTTP status and parsed response body.

import { useFormspark, FormsparkError } from "@formspark/use-formspark";

try {
  await submit({ message });
} catch (error) {
  if (error instanceof FormsparkError) {
    console.error(error.status, error.body);
  }
}

License

MIT