React Confetti Component

6.4.0 · active · verified Sun Apr 19

react-confetti is a React component designed to easily add customizable confetti effects to web applications. It provides a declarative way to render a shower of confetti, suitable for celebrations, achievements, or any event requiring a festive visual flourish. The current stable version is 6.4.0, with the project demonstrating an active release cadence, including multiple updates in early 2025 to add features, fix bugs, and ensure compatibility with newer React versions. Key differentiators include its simplicity as a React component, comprehensive TypeScript type definitions, and highly configurable properties such as `width`, `height`, `numberOfPieces`, and `confettiSource`. It's built to run efficiently in the browser, focusing solely on the visual confetti effect without external dependencies for physics or animation beyond what React and the browser provide, making it lightweight and easy to integrate.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to integrate `react-confetti` into a React application, dynamically adjusting to window size changes using a custom hook, and configuring various confetti properties. It also includes an example of programmatically stopping the confetti after a duration and a toggle button.

import React, { useState, useEffect } from 'react';
import Confetti from 'react-confetti';

// A simple hook to get window dimensions for demonstration
const useWindowSize = () => {
  const [windowSize, setWindowSize] = useState({
    width: window.innerWidth,
    height: window.innerHeight,
  });

  useEffect(() => {
    const handleResize = () => {
      setWindowSize({
        width: window.innerWidth,
        height: window.innerHeight,
      });
    };

    window.addEventListener('resize', handleResize);
    return () => window.removeEventListener('resize', handleResize);
  }, []);

  return windowSize;
};

export default function MyConfettiComponent() {
  const { width, height } = useWindowSize();
  const [showConfetti, setShowConfetti] = useState(true);

  // Stop confetti after 10 seconds for demonstration
  useEffect(() => {
    const timer = setTimeout(() => {
      setShowConfetti(false);
    }, 10000); // Confetti will stop after 10 seconds
    return () => clearTimeout(timer);
  }, []);

  return (
    <div style={{ position: 'relative', width: '100vw', height: '100vh', overflow: 'hidden' }}>
      <h1 style={{ textAlign: 'center', marginTop: '50px', color: '#673ab7' }}>Congratulations!</h1>
      <p style={{ textAlign: 'center', color: '#3f51b5' }}>This is a celebration with react-confetti.</p>
      {showConfetti && (
        <Confetti
          width={width}
          height={height}
          numberOfPieces={500}
          recycle={false} // Confetti will stop falling after all pieces have fallen once
          tweenDuration={2000} // Smooth transition for stopping
          gravity={0.1} // Lighter gravity for longer fall
          wind={0.05} // Slight wind effect
          colors={['#f44336', '#e91e63', '#9c27b0', '#673ab7', '#3f51b5']} // Custom colors
          initialVelocityX={{ min: -10, max: 10 }}
          initialVelocityY={{ min: -10, max: 10 }}
        />
      )}
      <button onClick={() => setShowConfetti(prev => !prev)} style={{ position: 'absolute', bottom: '20px', left: '50%', transform: 'translateX(-50%)', padding: '10px 20px', cursor: 'pointer', border: 'none', borderRadius: '5px', backgroundColor: '#3f51b5', color: 'white' }}>
        Toggle Confetti
      </button>
    </div>
  );
}

view raw JSON →