1.0.1 • Published 4 years ago

use-jsonp v1.0.1

Weekly downloads
3
License
MIT
Repository
github
Last release
4 years ago

use-jsonp

A custom React hook for making JSONP requests.

Installation

Install with npm:

$ npm install --save use-jsonp

(or yarn):

$ yarn add use-jsonp

Example

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>
    )
}