Skip to content

Instantly share code, notes, and snippets.

@thetrevorharmon
Last active April 5, 2021 19:30
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thetrevorharmon/6f308dc9f97aaa050f6e1424dce0890d to your computer and use it in GitHub Desktop.
Save thetrevorharmon/6f308dc9f97aaa050f6e1424dce0890d to your computer and use it in GitHub Desktop.
Gatsby Mailchimp Signup Form
import addToMailchimp from 'gatsby-plugin-mailchimp';
import React, { useState } from 'react';
import * as styles from './EmailListForm.module.scss';
const EmailListForm: React.FunctionComponent<{}> = () => {
const [email, setEmail] = useState('');
const handleSubmit = (e: React.FormEvent<HTMLFormElement>) => {
e.preventDefault();
addToMailchimp(email)
.then((data) => {
alert(data.result);
})
.catch((error: Error) => {
// Errors in here are client side
// Mailchimp always returns a 200
});
};
const handleEmailChange = (event: React.ChangeEvent<HTMLInputElement>) => {
setEmail(event.currentTarget.value);
};
return (
<form onSubmit={handleSubmit} className={styles.EmailListForm}>
<h2>Subscribe to my email list!</h2>
<div className={styles.Wrapper}>
<input
placeholder="Email address"
name="email"
type="text"
onChange={handleEmailChange}
/>
<button type="submit">Subscribe</button>
</div>
</form>
);
};
export default EmailListForm;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment