[Chartjs]-Using library without 'require' when using CDN

4👍

Just look at the file the CDN provides and find out for yourself.

It has this form:

(function (root, factory) {
    // Do some stuff to detect the environment
    // When everything fails, just assign a global variable:

    root["react-chartjs"] = factory(root["React"], root["ReactDOM"], root["Chart"]);


})(this, function ( ... ) {
    ....
});

Where root is the global object (window in the browser) and factory is just a function that takes the dependencies and returns the library.

To get LineChart:

window['react-chartjs'].Line

Keep in mind factory needs React, ReactDOM and Chart. And assumes all these will be in a global variable named the same way.

Leave a comment