npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

react-dependent-script

v1.1.2

Published

react-dependent-script React component

Downloads

7,283

Readme

ReactDependentScript

npm package

ReactDependentScript is a simple React component used to lazily load JavaScript and CSS resources when you need them.

When integrating with third party software it is often necessary to load that project's JavaScript or CSS on to the page. The naive way to do this is to always include it in the HTML of the page, but this means you pay the cost up front of loading that code, when your React component may never be even used.

ReactDependentScript is a React component that wraps your component's definition, and makes sure to load the JavaScript and/or CSS first, then render your content. It ensures that the external content is only loaded once, regardless of how many times the render() function is called.

Using it

First install with

npm install react-dependent-script

or

yarn add react-dependent-script

Now wrap your custom content in the component, providing an array of URLs to scripts to load, and optionally a loadingComponent property containing a component to show while loading e.g.

import ReactDependentScript from 'react-dependent-script';
<ReactDependentScript
  loadingComponent={<div>jQuery is loading...</div>}
  scripts={['https://code.jquery.com/jquery-3.2.1.min.js']}
>
  <div>jQuery is loaded!</div>
</ReactDependentScript>

An alternative to rendering the child components is to provide a callback function to the renderChildren prop. This can be useful when you need to execute code that is only available after the remote script is loaded, e.g.

import ReactDependentScript from 'react-dependent-script';
<ReactDependentScript
  loadingComponent={<div>jQuery is loading...</div>}
  scripts={['https://code.jquery.com/jquery-3.2.1.min.js']}
  renderChildren={() => {
    return <div>Found {$('*').length} DOM nodes on the page</div>
  }}
>
</ReactDependentScript>

You can also load other interesting libraries, like Stripe

import ReactDependentScript from 'react-dependent-script';
<ReactDependentScript
  loadingComponent={<div>Loading Stripe...</div>}
  scripts={['https://js.stripe.com/v3/']}
>
  <div>Stripe script is loaded, here is your card!</div>
  <StripeProvider apiKey="pk_test_YOUR_KEY_HERE">
    <Elements>
      <CheckoutFormElements
        submitText="Submit"
        onTokenCreated={data => {
          console.log('token created', data);
        }}
        onTokenCreationFailed={data => {
          console.log('token failed', data);
        }}
      />
    </Elements>
  </StripeProvider>
</ReactDependentScript>

See the demo/src/index.js file for more complex examples, including loading CSS files.

Contributing

This project uses the nwb module for development. After checking out the code, you have three commands available to you:

  • build builds the code into the es and lib folders. You generally don't need these.
  • start starts the demo page, which is running the demo/src/index.js code. This is your usual development environment, where you'll write examples to validate any changes.
  • clean cleans out all build artifacts