-
-
Notifications
You must be signed in to change notification settings - Fork 9.5k
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
How to load an external scripts #334
Comments
Since we are running inside an inframe, you wouldnt see it directly in the console. Make sure, we only run a component once. Hooks like componentWillRecieveProps won't work. When you change a story, we will run it from the begining after cleaning the DOM. |
@arunoda Nice! Works fine to me :) |
What if you need a cdn resource before the stories run/mount? var App = (function () {
return function () {
loadScript('cdn.file.js' , function scriptsDidLoadCallback () {
storiesOf('Button Test', module).add('', () => (<Button/>));
});
}
})();
App(); which assumes the setup is something like this: function loadScript (file, callback) {
source = document.createElement('script');
source.src = file;
source.async = true;
// ...
source.onload = function () {
callback (); // aka: scriptsDidLoadCallback
};
} |
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. |
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; |
I created a public decorator to solve it: It also works for Vue. |
Thank you for sharing that snippet 🙏 I'm struggling to get it to work properly though. Could you please share an example of how to use that decorator in a Story? |
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: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.The text was updated successfully, but these errors were encountered: