You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I was recently tasked with implementing mailchimp subscription in react, but I found the implementation of MailchimpSubscrbe to be rather inconvenient for properly managing state.
The problem
In our case, we use react-google-recaptcha and react-final-form, but the problem is that final form expects that once we return from the onSubmit function, that the form has been submitted. Inside the onSubmit function, we wait for the recaptcha to complete, but when it comes to the subscribe callback exposed by MailchimpSubscribe, it does not return a promise; So calling that function inside the onSubmit function will immediately return which leads the form to assume the subscription was successful.
Solution
I came up with a higher-order component which simply exposes a function that returns a promise and you don't have to worry about status; Just wait for the function to complete and if there is an error at the end, you can "catch" that and do something with it. Here it is (hope you don't mind typescript 😄 ):
/* eslint-disable react-hooks/rules-of-hooks */importReact,{useCallback,useEffect,useRef}from"react";importMailchimpSubscribe,{DefaultFormFields}from"react-mailchimp-subscribe";interfaceMailchimpSubscribeHOCProps<MCF>{mailchimpSubscribe: (formFields: MCF)=>Promise<void>;}/** * Wraps a component with the MailchimpSubscribe component and exposes a simple subscribe * function which returns a promise * * @param Wrapped The component to wrap * @param mailchimpSubscribeUrl The POST url used for subscribing via mailchimp * @returns A component which has the ability to subscribe to */exportdefaultfunctionwithMailchimpSubscribe<T={},MCF=DefaultFormFields>(Wrapped: React.ComponentType<T&MailchimpHOCProps<MCF>>,mailchimpSubscribeUrl: string): React.FC<T>{constsubscribeSuccess=useRef<()=>void>(null);constsubscribeError=useRef<(error?: any)=>void>(null);useEffect(()=>()=>{// Avoid memory leaks(?) by removing any held referencessubscribeSuccess.current=null;subscribeError.current=null;},[]);// eslint-disable-next-line react-hooks/rules-of-hooksreturnuseCallback((props: T)=>(<MailchimpSubscribe<MCF>url={mailchimpSubscribeUrl}render={({ subscribe, status, message })=>{constmailchimpSubmit: (formFields: MCF)=>Promise<void>=(formFields: MCF)=>{subscribe(formFields);returnnewPromise<void>((resolve,reject)=>{subscribeSuccess.current=resolve;subscribeError.current=reject;});};if(status==='success'&&subscribeSuccess.current!=null){subscribeSuccess.current();}elseif(status==='error'&&subscribeError.current!=null){subscribeError.current(message);}return<WrappedmailchimpSubscribe={mailchimpSubmit}{...props}/>;}}/>// eslint-disable-next-line react-hooks/exhaustive-deps),[mailchimpSubscribeUrl]);}
The promise resolves to nothing if the subscription was successful, otherwise it fails with an error message if the status is error.
Example
constmailchimpUrl="...";constMyCustomForm: React.FC=withMailchimpSubscribe(({mailchimpSubscribe: subscribe})=>{// do some state management herereturn(<Form.../>
);
},mailchimpUrl);
The text was updated successfully, but these errors were encountered:
I was recently tasked with implementing mailchimp subscription in react, but I found the implementation of
MailchimpSubscrbe
to be rather inconvenient for properly managing state.The problem
In our case, we use
react-google-recaptcha
andreact-final-form
, but the problem is that final form expects that once we return from theonSubmit
function, that the form has been submitted. Inside theonSubmit
function, we wait for the recaptcha to complete, but when it comes to the subscribe callback exposed byMailchimpSubscribe
, it does not return a promise; So calling that function inside theonSubmit
function will immediately return which leads the form to assume the subscription was successful.Solution
I came up with a higher-order component which simply exposes a function that returns a promise and you don't have to worry about
status
; Just wait for the function to complete and if there is an error at the end, you can "catch
" that and do something with it. Here it is (hope you don't mind typescript 😄 ):The promise resolves to nothing if the subscription was successful, otherwise it fails with an error message if the status is error.
Example
The text was updated successfully, but these errors were encountered: