A custom React hook for making JSONP requests.
Install with npm:
$ npm install --save use-jsonp
(or yarn):
$ yarn add use-jsonp
Make a request to a server that supports jsonp.
import React from 'react';
import useJSONP from 'use-jsonp';
const MailchimpForm = () => {
type MailchimpResponse = {
msg: string
result: "success" | "error"
}
const sendJsonP = useJSONP<MailchimpResponse>({
url: 'https://somemailchimpaccount.us12.list-manage.com/subscribe/post-json?u=###&FNAME=bobby&EMAIL=bobby@somedomain.com',
id: 'mailchimpScript,
callback: data => console.log(data),
callbackParam: "c",
})
return (
<form onSubmit={() => sendJsonP()}>...</form>
)
}