Git Product home page Git Product logo

react-script-hook's Introduction

react-script-hook

React hook to dynamically load an external script and know when its loaded

Install

// with npm
npm install react-script-hook

// with yarn
yarn add react-script-hook

How to use

import React from 'react';
import { StripeProvider } from 'react-stripe-elements';
import useScript from 'react-script-hook';

import MyCheckout from './my-checkout';

function App() {
    const [loading, error] = useScript({ src: 'https://js.stripe.com/v3/' });

    if (loading) return <h3>Loading Stripe API...</h3>;
    if (error) return <h3>Failed to load Stripe API: {error.message}</h3>;

    return (
        <StripeProvider apiKey="pk_test_6pRNASCoBOKtIshFeQd4XMUh">
            <MyCheckout />
        </StripeProvider>
    );
}

export default App;

Use with callbacks

useScript({
    src: 'https://js.stripe.com/v3/',
    onload: () => console.log('Script loaded!'),
});

Check for Existing

If you're in an environment where the script may have already been loaded, pass the checkForExisting flag to ensure the script is only placed on the page once by querying for script tags with the same src. Useful for SSR or SPAs with client-side routing.

const [loading, error] = useScript({
    src: 'https://js.stripe.com/v3/',
    checkForExisting: true,
});

Conditionally calling a script

If you want to conditionally call a script you can do so by setting the src value to either the script or null. Note that the script is not removed if the src is set to null after it was previously set to a value causing the script to be appended.

const [loading, error] = useScript({
    /** Only append stripeJS script when shouldLoadStripe is true */
    src: shouldLoadStripe ? 'https://js.stripe.com/v3/' : null,
});

License

MIT

react-script-hook's People

Contributors

hupe1980 avatar a-tokyo avatar dependabot[bot] avatar hartzis avatar dunncl15 avatar chrisdrackett avatar lyallh avatar nextlevelshit avatar timruffles avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.