How to integrate mailchimp with React

Search for a command to run...

No comments yet. Be the first to comment.
The best talent comes from referrals. So how do you convince people to share their referrals? You reward them with cash. Problem now, how do you avoid people sharing “referrals” mindlessly? You vet them. Reach out to your network, ask if they know s...
If you've faced this situation before, you might want to learn this. You're working on feature A at your work and suddenly you got a ping on Slack. "Please fix this bug, it's causing a critical bug in production that needs fixing ASAP!!!" You git st...
Before we start, please read the QRTs from both Edd and Mu'azh. They both have presented a great point about luck Keep working and grinding so that you're prepared to take advantage of luck when it comes. Me however, I'm here to share about how t...
Open the PDF file in Chromium-based browsers Print as PDF https://twitter.com/afrieirham_/status/1681196950715088897
If you're like me, you've probably heard this advice a lot, especially in the tech industry. "Go do more interviews, even especially if you're not actively looking." And they're right, here's why. You have higher leverage in negotiation when you're n...
I hope I make it clear enough what this article is all about based on the title. But to add on the title a little more, this article will work with any frontend JavaScript framework.
If you ever find yourself in the same position as me, ie. you want to use mailchimp to collect email and you want to connect it to a custom frontend project, this article will be useful to you.
Note that I will be using React in this article as an example, but this should work with any JavaScript project.
If you want to have a look at my implementation, this is the source code. Also, this article will assume that you've already setup your project.
With that out of the way, let's get into the article.
What you'll need:
Go to your Mailchimp dashboard, on the sidebar, go to Audience > Signup forms. And then, click on Embeded forms.
You can add more fields by clicking on the Form Fields and enable all the fields that you need. Then click on Continue.
Find the form tag in the code, and copy the action URL.
Also, look for input tag and find the fields that you've added. Take note the name of the input tag. We will be using it to submit our form later.
<input type="email" value="" name="EMAIL" class="required email" id="mce-EMAIL" required>
In this case, the name is EMAIL.
onSubmit handlerBefore we proceed: Don't forget to install jsonp package in your project.
First, we need to replace the /post? with /post-json? in our url. Then add this code in your onSubmit handler. I'll explain what we did later.
const onSubmit = e => {
e.preventDefault();
const url = 'your-mailchimp-url';
jsonp(`${url}&EMAIL=${email}`, { param: 'c' }, (_, data) => {
const { msg, result } = data
// do something with response
alert(msg);
});
};
We use the jsonp function to make the request. We then append &EMAIL=<your-input-email> to include the email that we want to submit.
If you have more fields, just use the name value you've listed out from Mailchimp code before, and append it accordingly.
Then we add the { param: 'c' } as the second parameter, but I don't really know why we need this to be honest.
Lastly, we add a callback function to handle the response. The relevant response data will be available to you in the second argument. That's why I use _ for the first argument.
From there, I just alert the user with the response message.
And that's it, you should be able to submit the email to Mailchimp now.
I write this article to share my learning when I was building "Coming Soon" page for my upcoming project sea-techjobs.com. (shameless plug here ✊)
I want to collect emails using Mailchimp but I don't want to use their form builder. I think I can do a better job with React and ChakraUI to design the website but I wasn't sure how to exactly get this two connected.
I first stumbled upon this video on how to setup Mailchimp newsletter with React. And in that video, they were using this library called react-mailchimp-subscribe but I couldn't get the state working right.
I looked into the source code and figure out exactly how it works under the hood. I realized that I don't need that library for it to work. I can just copy how it works and tweak it to suit my needs.
Then I thought, why not write an article to hopefully help other people who might be facing the same issue – thus this article.
So I guess that's it from me. Thank you for reading and have a good day.
p/s - If you're curious about the project that I'm working on, you can read this twitter thread to learn more. It's in Malay though, I'm sorry if you're english reader. 🤧