0

I am using "react-mailchimp-subscribe" to integrate with MailChimp and enable users to signup to a mailchimp mailing list upon completing an submitting my form.

However, I get the error:

Uncaught TypeError: onValidated is not a function

When I trigger the function that should submit the form info to MailChimp.

Here is a sample of my code:

import MailchimpSubscribe from "react-mailchimp-subscribe";
...
const CustomForm = ({
  className,
  topOuterDivider,
  bottomOuterDivider,
  topDivider,
  bottomDivider,
  hasBgColor,
  invertColor,
  split,
  status,
  message,
  onValidated,
  ...props
}) => {
...
  const [email, setEmail] = useState('');
  const [fullName, setFullName] = useState('');
  const handleSubmit = (e) => {
    console.log('handlesubmit triggered')
    e.preventDefault();
    email &&
    fullName &&
    email.indexOf("@") > -1 &&
    onValidated({
        EMAIL: email,
        MERGE1: fullName,
    });
  }
...
<form onSubmit={(e) => handleSubmit(e)}>
        <h3>
            {status === "success"
                ? "You have successfully joined the mailing list"
                : "Join our mailing list by completing the form below"
            }
        </h3>
        <div className="cta-action">
            <Input
                id="email"
                type="email"
                label="Subscribe"
                labelHidden hasIcon="right"
                placeholder="Your email address"
                onChange={e => setEmail(e.target.value)}
                value={email}
                isRequired
                >
            </Input>
            <Input
                id="name"
                type="text"
                label="Subscribe"
                labelHidden hasIcon="right"
                placeholder="Your full name"
                onChange={e => setFullName(e.target.value)}
                value={fullName}
                isRequired
                >
            </Input>
            <Input
                type="submit"
                formValues={[email, fullName]}
            />
        </div>
    </form>

This is the parent component passing down props to the above:

import React from "react";
import MailchimpSubscribe from "react-mailchimp-subscribe";

const MailchimpFormContainer = props => {

    const postURL = `https://*li$%.list-manage.com/subscribe/post?u=${process.env.REACT_APP_MAILCHIMP_U}$id=${process.env.REACT_APP_MAILCHIMP_ID}`;
    return (
        <div>
        <MailchimpSubscribe
            url={postURL}
            render={({ subscribe, status, message }) => (
                <CustomForm
                    status={status}
                    message={message}
                    onValidated={ formData => subscribe(formData) }
                />
            )}
        />
        </div>
    );
};
Emm
  • 2,367
  • 3
  • 24
  • 50
  • What `props` are set for this `CustomForm` component? Could you show where the `CustomForm` is being rendered, and what is passed to it. A simple answer would be to `console.log` the `onValidation` prop at the after your `setState`'s. That should tell you what `onValidation` is – lua_python_java Oct 12 '22 at 15:44
  • @lua_python_java I am supposed to pass in onValidated as a prop according to the docs. If I console log onValidated I get undefined: https://github.com/revolunet/react-mailchimp-subscribe/blob/master/demo/src/index.js – Emm Oct 12 '22 at 15:54
  • @lua_python_java I added the component where the props are set – Emm Oct 12 '22 at 15:57
  • 1
    I see, now is the time to debug. First try adding a `console.log` to the Parent component. Change the `onValidated={ formData => subscribe(formData) }` to `onValidated={ (formData) => { console.log("Can we even log this?"); subscribe(formData) } }` - This will likely NOT log to the console because `onValidated` is coming back `undefined`. – lua_python_java Oct 12 '22 at 16:43
  • One more thing, is the `CustomForm` rendering with the `message` and `status`? – lua_python_java Oct 12 '22 at 16:46
  • Everything looks good and pretty identical to the demo, except all of the extra `props` being sent to the `CustomForm` component. I would try, just to see, removing all the props except the `message`, `status`, and `onValidated`. If you can get all three `console.log`'d as expected (inside the `CustomForm`), then start adding back all the other props. Besides that I am not sure why the property `onValidated` wouldn't be getting passed in... – lua_python_java Oct 12 '22 at 16:52
  • @lua_python_java So I realized that I was initially having trouble reading env variables that I was using to POST this to mailchimp. It seems now, I send the data, receive 'sending' as the status. This eventually timesout, failing to subscribe the user – Emm Oct 12 '22 at 16:54
  • Good job, the thing that effects the thing, that effects the thing, etc are the hardest problems to solve. If you wanna thumbs up one of my comments, that'd be nice! (: – lua_python_java Oct 12 '22 at 17:13

0 Answers0