question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to load an external scripts

See original GitHub issue

Hello,

Today I’m try to use the react-storybook for building a component thats use the SDK of Facebook. I will try something like this:

componentWillMount() {
  (function(d, s, id){
     var js, fjs = d.getElementsByTagName(s)[0];
     if (d.getElementById(id)) {return;}
     js = d.createElement(s); js.id = id;
     js.src = "//connect.facebook.net/en_US/sdk.js";
     fjs.parentNode.insertBefore(js, fjs);
  }(document, 'script', 'facebook-jssdk'));
}

And I did not succeed. The script loads up if I see on the network of browser. But it is not accessible through the console or even within React functions.

I believe it can be related to how the encapsulation react-storybook works.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
3mcdcommented, Oct 23, 2018

Leaving this snippet here for anyone who may have a similar need to load scripts per-story:

import React, { Component } from 'react';
import PropTypes from 'prop-types';

class Script extends Component {
  static propTypes = {
    src: PropTypes.string.isRequired,
  };

  state = {
    loaded: false,
  };

  componentWillMount() {
    const head = document.querySelector('head');
    const script = document.createElement('script');

    script.async = true;
    script.src = this.props.src;
    script.onload = () => this.setState({
      loaded: true,
    });

    head.appendChild(script);
  }

  render() {
    return this.state.loaded ? this.props.children : null;
  }
}

const loadScriptDecorator = src => story => <Script src={src}>{story()}</Script>;

export default loadScriptDecorator;
1reaction
jonnybot0commented, Sep 23, 2018

I need something like what @adamellsworth described, but his code didn’t seem to work. While I didn’t get any errors, my stories weren’t actually loaded at runtime.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to Load External JavaScript Files From the Browser ...
Using appendChild() is a more useful way to load an external script file. Instead of overwriting the loaded page with document.write() , ...
Read more >
HTML script src Attribute - W3Schools
The src attribute specifies the URL of an external script file. If you want to run the same JavaScript on several pages in...
Read more >
The best way to load external JavaScript - Human Who Codes
The best technique · Create two JavaScript files. · Include the first JavaScript file with a <script> tag at the bottom of the...
Read more >
How to load an external JavaScript on a condition
The question is how to rewrite a static HTML-code into JavaScript which decides whether to load the script and loads it programmatically.
Read more >
How to Load External Scripts Dynamically In Angular
The easiest way to add external dependencies to your Angular projects is through npm. The second best option is to add script tags...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found