Custom mailchimp signup form in React

前端 未结 4 1811
后悔当初
后悔当初 2021-02-05 09:54

I have a react app that I would like to add mailchimp signup form to. I am building a custom signup form and have user sign up by entering their first and last name and email. I

4条回答
  •  自闭症患者
    2021-02-05 10:22

    It is possible. [1]

    As you mentioned in your link, MailChimp allows you to post the values of form elements on our client to subscribe people to your mailing list. Anything more advanced than that, and you need to use their API, thus requiring CORS (i.e. the API can't be utilized by the client).

    If you aren't using React – you can simply copy/paste MailChimp's embedded form builder. But if you are in React, here's what you can do:

    1) Get the u and id value

    This is outlined in the link above. Or, You can go into your form builder and find the action field: it will be something like https://cool.us16.list-manage.com/subscribe/post?u=eb05e4f830c2a04be30171b01&id=8281a64779 where u and id are in the query arguments.

    2) Add onChange and value attributes to form elements

    If you just have some simple form elements, you'll notice that typing doesn't do anything. Typing doesn't render any text inside the input elements. Read https://reactjs.org/docs/forms.html to understand why. (React needs a single source of truth for state).

    The value needs to be updated. You can do this by updating state in a function that's bind'd.

    {this.setState({emailValue: e.target.value});} } 
    /> 
    

    3) Bonus: include MailChimp's antispam fields

    If you read the code inside the embedded forms, you'll notice things like:

    
    
    

    You can include these in React by changing the appropriate fields for Babel and JSX. class needs to be className. the tag needs to be closed, and style needs to be passed as an object.

    In sum: here's an entire component

    import React from 'react'
    
    class SubscribePage extends React.Component {
      constructor(props) {
        super(props);
        this.state = {
            emailValue: '',
            fNameValue: '',
            lNameValue: '',
        };
      }
    
        render() {
            return (
                    
    ) } } export default SubscribePage

    [1]: Note that the user will be taken to a MailChimp page, and asked to verify the subscription by clicking on a link in their email. To avoid this, you gotta use the API

提交回复
热议问题