• Stars
    star
    361
  • Rank 117,957 (Top 3 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 8 years ago
  • Updated almost 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

React component that makes it easy to load 3rd party scripts

react-load-script CircleCI Dependency Status

This package simplifies loading of 3rd party scripts in your React applications.

Motivation

There are situations when you need to use a 3rd party JS library in your React application (jQuery, D3.js for rendering charts, etc.) but you don't need it everywhere and/or you want to use it only in a response to users actions. In cases like this, preloading the whole library when application starts is an unnecessary and expensive operation which could possibly slow down your application.

Using the Script component this package provides you with, you can easily load any 3rd party scripts your applications needs directly in a relevant component and show a placeholder while the script is loading (e.g. a loading animation). As soon as the script is fully loaded, a callback function you'll have passed to Script is called (see example below).

Supported React versions

This package requires React 0.14.9 and higher.

API

The package exports a single component with the following props:

onCreate

Called as soon as the script tag is created.

onError (required)

Called in case of an error with the script.

onLoad (required)

Called when the requested script is fully loaded.

url (required)

URL pointing to the script you want to load.

attributes

An object used to define custom attributes to be set on the script element. For example, attributes={{ id: 'someId', 'data-custom: 'value' }} will result in <script id="someId" data-custom="value" />

Example

You can use the following code to load jQuery in your app:

import Script from 'react-load-script'

...

render() {
  return (
    <Script
      url="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"
      onCreate={this.handleScriptCreate.bind(this)}
      onError={this.handleScriptError.bind(this)}
      onLoad={this.handleScriptLoad.bind(this)}
    />
  )
}

...

handleScriptCreate() {
  this.setState({ scriptLoaded: false })
}

handleScriptError() {
  this.setState({ scriptError: true })
}

handleScriptLoad() {
  this.setState({ scriptLoaded: true })
}

License

MIT 2016

Made with love by

More Repositories

1

react-bluekit

Automatically generating a component library from your React components (ES5, ES6, Typescript)
JavaScript
653
star
2

redux-file-upload

Redux-friendly file upload made easy
JavaScript
67
star
3

react-svg-icon-generator

Generate React Icon Component from SVG icons to show, resize and recolor them.
JavaScript
65
star
4

onion-form

React Redux form builder with great UX validations
JavaScript
50
star
5

vcr.js

Mock server with Proxy and Record support inspired by ruby VCR.
TypeScript
42
star
6

ts-translate

Translate, Interplate, format text into different localisations for React applications.
TypeScript
34
star
7

translation-server

Stores translations with location and screenshot. Enable users to easily edit translations and then any rails/react application can use them.
Ruby
27
star
8

detect-adblock

Get information about whether the user has enabled AdBlock
JavaScript
25
star
9

redux-preload

Preload actions on both server & client side (allows deep nesting of components)
JavaScript
21
star
10

radium-flex

Radium Flex Box React Components Grid & Box
TypeScript
19
star
11

haystack

Core developer stack for JavaScript applications with server side rendering, code splitting, webpack 3
JavaScript
18
star
12

radium-bootstrap-grid

Bootstrap grid system as React components with inline styles!
JavaScript
18
star
13

js-package-template

Boilerplate for creating a new javascript package
Shell
17
star
14

translation-engine

A Rails engine for sending and receiving translations from Translation Server
JavaScript
14
star
15

blueberry_rails

A Rails application template used at Blueberry Apps.
Ruby
9
star
16

radium-styled-component

A Radium React component for managing global variables for inline styles.
JavaScript
9
star
17

react-svg-icon-live-generator

Live SVG Icon React Component generator website
JavaScript
9
star
18

generate-unit-test

Generates basic unit test for mocha, sinon and chai
JavaScript
5
star
19

gp_webpay

Ruby
4
star
20

devise-login_tracker

Devise strategy that tracks each login with IP address and User agent.
Ruby
3
star
21

bb-toggl

JavaScript
2
star
22

bb-car

JavaScript
2
star
23

js-resources

Resources for JavaScript developers
2
star
24

graphql-playground

JavaScript
2
star
25

netverify-engine

Jumio Netverify integration into Rails app as an engine
Ruby
2
star
26

riga-training-examples

Examples for React.js Riga training
HTML
2
star
27

gulp-font-icons

JavaScript
2
star
28

netverify

Simple integration of Jumio Netverify and Netverify Multi Document for Ruby applications
Ruby
1
star
29

ruby-on-rails-second-round-frontend

Front-end part of our interview test application.
JavaScript
1
star
30

guides

Blueberry Guides
1
star
31

react-lessons

JavaScript
1
star
32

react-bluekit-web

Web for react-bluekit
JavaScript
1
star
33

aloe

Aloe is a Rails engine that provides double entry bookkeeping functionality.
Ruby
1
star