Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/means88/react-jsport

Load external JavaScript&CSS in React
https://github.com/means88/react-jsport

npm react

Last synced: about 10 hours ago
JSON representation

Load external JavaScript&CSS in React

Awesome Lists containing this project

README

        

React JSPort [![react-jsport](https://badge.fury.io/js/react-jsport.svg)](https://www.npmjs.com/package/react-jsport)
===

```
npm install react-jsport
```

DEMO
---
[https://means88.github.io/react-jsport/](https://means88.github.io/react-jsport/)

INTERFACE
---
```js
function load(requirements = [], options = {});
/**
* @param requirements: array of url[dfault=[]]
* @param options object[default={}]: {
* force: boolean[default=false], requirements with the same url will not be loaded twice by default
* loadingElement: ReactElement[default=null], be rendered while loading
* errorElement: ReactElement[default=null], be rendered when an error caused
* }
* @return ReactComponent
*/

JSPort.propTypes = {
// requirements, can be url or list of url
require: PropTypes.oneOfType([
PropTypes.string,
PropTypes.arrayOf(PropTypes.string),
]),
// load requirements even if it has been loaded
force: PropTypes.bool,
children: PropTypes.oneOfType([
PropTypes.element,
PropTypes.arrayOf(PropTypes.element)
]),
loadingElement: PropTypes.element,
errorElement: PropTypes.element,
};

JSPort.defaultProps = {
require: [],
force: false,
children: null,
loadingElement: null,
errorElement: null,
};

load(requirements, options)(Component);
// ==>

```

USAGE
---

```js
import { load } from 'react-jsport';

Component = load("https://cdn.bootcss.com/jquery/2.2.4/jquery.min.js")(Component);

// or

@load("https://cdn.bootcss.com/jquery/2.2.4/jquery.min.js")
class Component extends React.Component {
// ...
}
```

Or wrapped in `JSPort`

```js
class Component extends React.Component {

componentDidMount() {
this.changeText();
}

componentDidUpdate() {
this.changeText();
}

changeText() {
$('#hello').text('Changed by jQuery');
}

render() {
return (

Hello world

);
}
}

// import jQuery dynamically
import JSPort from 'react-jsport';

```

TODO
---
- cache