Back to Integrations
React logo

React Integration Guide

A JavaScript library for building user interfaces with component-based architecture.

Quick Start

Get started with React and StaticBox in minutes:

npx create-react-app my-staticbox-app

Setup Steps

1

Create React App

Start with a new React application or use your existing one.

npx create-react-app my-staticbox-app
cd my-staticbox-app
npm start
2

Create Contact Form Hook

Create a custom hook for form submission logic.

// hooks/useContactForm.js
import { useState } from 'react';

export const useContactForm = () => {
  const [isSubmitting, setIsSubmitting] = useState(false);
  const [isSubmitted, setIsSubmitted] = useState(false);
  const [error, setError] = useState(null);

  const submitForm = async (formData) => {
    setIsSubmitting(true);
    setError(null);
    
    try {
      const response = await fetch('https://staticbox.io/api/forms/[your-project-id]/[slug-name]/submit', {
        method: 'POST',
        body: formData,
      });
      
      if (response.ok) {
        setIsSubmitted(true);
      } else {
        throw new Error('Failed to submit form');
      }
    } catch (err) {
      setError(err.message);
    } finally {
      setIsSubmitting(false);
    }
  };

  return { isSubmitting, isSubmitted, error, submitForm };
};
3

Create Form Component

Build a reusable contact form component.

// components/ContactForm.js
import React from 'react';
import { useContactForm } from '../hooks/useContactForm';

export const ContactForm = () => {
  const { isSubmitting, isSubmitted, error, submitForm } = useContactForm();

  const handleSubmit = (e) => {
    e.preventDefault();
    const formData = new FormData(e.target);
    submitForm(formData);
  };

  if (isSubmitted) {
    return (
      <div className="success-message">
        <h3>Thank you!</h3>
        <p>Your message has been sent successfully.</p>
      </div>
    );
  }

  return (
    <form onSubmit={handleSubmit}>
      <div>
        <label htmlFor="name">Name</label>
        <input type="text" id="name" name="name" required />
      </div>
      <div>
        <label htmlFor="email">Email</label>
        <input type="email" id="email" name="email" required />
      </div>
      <div>
        <label htmlFor="message">Message</label>
        <textarea id="message" name="message" required />
      </div>
      <button type="submit" disabled={isSubmitting}>
        {isSubmitting ? 'Sending...' : 'Send Message'}
      </button>
      {error && <p className="error">{error}</p>}
    </form>
  );
};

Advanced Examples

With React Hook Form

Integrate StaticBox with React Hook Form for better form handling.

npm install react-hook-form

// components/ContactForm.js
import { useForm } from 'react-hook-form';

export const ContactForm = () => {
  const { register, handleSubmit, reset, formState: { isSubmitting } } = useForm();

  const onSubmit = async (data) => {
    const formData = new FormData();
    Object.keys(data).forEach(key => {
      formData.append(key, data[key]);
    });

    const response = await fetch('https://staticbox.io/api/forms/[your-project-id]/[slug-name]/submit', {
      method: 'POST',
      body: formData,
    });

    if (response.ok) {
      reset();
      alert('Message sent successfully!');
    }
  };

  return (
    <form onSubmit={handleSubmit(onSubmit)}>
      <input {...register('name', { required: true })} placeholder="Name" />
      <input {...register('email', { required: true })} type="email" placeholder="Email" />
      <textarea {...register('message', { required: true })} placeholder="Message" />
      <button type="submit" disabled={isSubmitting}>
        {isSubmitting ? 'Sending...' : 'Send'}
      </button>
    </form>
  );
};

Deployment

Deploy to Netlify

  1. 1
    Build your React app with npm run build
  2. 2
    Connect your repository to Netlify
  3. 3
    Set build command to "npm run build" and publish directory to "build"
  4. 4
    Your forms will work in production automatically

Additional Resources

Ready to get started?

Create your StaticBox project and start building with React today.

Create Project