### Install use-formspark with NPM or Yarn Source: https://github.com/formspark/use-formspark/blob/master/README.md Install the library using either NPM or Yarn package managers. ```bash # NPM npm install @formspark/use-formspark # Yarn yarn add @formspark/use-formspark ``` -------------------------------- ### Handle FormsparkError in Newsletter Subscription Source: https://context7.com/formspark/use-formspark/llms.txt Demonstrates how to use the `useFormspark` hook for a newsletter subscription and specifically catch and handle `FormsparkError` instances. It provides examples for different HTTP status codes like 404 and 422. ```tsx import { useFormspark, FormsparkError } from "@formspark/use-formspark"; const NewsletterForm = () => { const [submit, submitting] = useFormspark({ formId: "newsletter-xyz" }); const handleClick = async () => { try { const response = await submit({ email: "user@example.com" }); console.log("Success:", response); // response is the parsed JSON returned by Formspark } catch (error) { if (error instanceof FormsparkError) { switch (error.status) { case 404: console.error("Form not found. Check your formId."); break; case 422: console.error("Validation error:", error.body); break; default: console.error(`Server error ${error.status}:`, error.body); } } } }; return ( ); }; ``` -------------------------------- ### Basic Form Submission with useFormspark Source: https://github.com/formspark/use-formspark/blob/master/README.md Use the useFormspark hook to manage form state and submission. Requires a form ID and handles submitting form data. ```tsx 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 (
{ e.preventDefault(); await submit({ message }); }}>