DEV Community

Cover image for Embed Substack Signup Form in React
Akilesh
Akilesh

Posted on

Embed Substack Signup Form in React

" This article is published using Lamento🍋

I have used substackapi to achieve this Custom Embed Substack Signup Form

Configuration:

substackUrl: "akileshio.substack.com",
Enter fullscreen mode Exit fullscreen mode

Image description

  • theme: To customize colors you can use pre-defined style purple, orange or green in theme or if you wanted to add your own colors scheme add custom in theme and add your colors to primary, input, email & text
theme: "custom",
colors: {
        primary: "#FFFFFF",
        input: "#334155",
        email: "#FFFFFF",
        text: "#000000",
      }
Enter fullscreen mode Exit fullscreen mode

Here is your final code:

import React, { useEffect } from 'react';

const SubstackWidget = () => {
  useEffect(() => {
    // Define the CustomSubstackWidget on the window object
    window.CustomSubstackWidget = {
      substackUrl: "akileshio.substack.com",
      placeholder: "example@gmail.com",
      buttonText: "Subscribe",
      theme: "custom",
      colors: {
        primary: "#FFFFFF",
        input: "#334155",
        email: "#FFFFFF",
        text: "#000000",
      },
    };

    // Create a new script element
    const script = document.createElement('script');

    // Set the source of the script to the Substack widget script
    script.src = "https://substackapi.com/widget.js";
    script.async = true;

    // Append the script to the body
    document.body.appendChild(script);

    // Cleanup function to remove the script when the component unmounts
    return () => {
      document.body.removeChild(script);
    }
  }, []);

  return (
    <div id="custom-substack-embed"></div>
  );
};

export default SubstackWidget;
Enter fullscreen mode Exit fullscreen mode

I you want to make your users redirect to custom URL after they signup, visit (substackapi)[https://substackapi.com/] to Unlock Custom Redirect for $3.99/month.

Top comments (3)

Collapse
 
nathan_tarbert profile image
Nathan Tarbert

Thanks, this is helpful!

Collapse
 
akilesh profile image
Akilesh

Your welcome, keep an eye out for useful content ☺️

Collapse
 
liamsherwood profile image
Liamsherwood

This did not work for me. In my nextJS project, I use the Custom Substack Embed as you set it up here, as a CSR component which i import elsewhere. The

is empty in my elements tab. Any ideas as to why this could be?