async-reactor
Advanced tools
Comparing version 1.0.3 to 1.0.4
@@ -1,1 +0,1 @@ | ||
'use strict';var _createClass=function(){function defineProperties(target,props){for(var descriptor,i=0;i<props.length;i++)descriptor=props[i],descriptor.enumerable=descriptor.enumerable||!1,descriptor.configurable=!0,'value'in descriptor&&(descriptor.writable=!0),Object.defineProperty(target,descriptor.key,descriptor)}return function(Constructor,protoProps,staticProps){return protoProps&&defineProperties(Constructor.prototype,protoProps),staticProps&&defineProperties(Constructor,staticProps),Constructor}}();exports.__esModule=!0,exports.Reactactor=void 0;exports.asyncReactor=asyncReactor;var _react=require('react');function _classCallCheck(instance,Constructor){if(!(instance instanceof Constructor))throw new TypeError('Cannot call a class as a function')}function _possibleConstructorReturn(self,call){if(!self)throw new ReferenceError('this hasn\'t been initialised - super() hasn\'t been called');return call&&('object'==typeof call||'function'==typeof call)?call:self}function _inherits(subClass,superClass){if('function'!=typeof superClass&&null!==superClass)throw new TypeError('Super expression must either be null or a function, not '+typeof superClass);subClass.prototype=Object.create(superClass&&superClass.prototype,{constructor:{value:subClass,enumerable:!1,writable:!0,configurable:!0}}),superClass&&(Object.setPrototypeOf?Object.setPrototypeOf(subClass,superClass):subClass.__proto__=superClass)}function isFunction(p){return'function'==typeof p}function isPromise(){var p=0<arguments.length&&arguments[0]!==void 0?arguments[0]:{};return isFunction(p.then)}var Reactactor=exports.Reactactor=function(_Component){function Reactactor(props){_classCallCheck(this,Reactactor);var _this=_possibleConstructorReturn(this,(Reactactor.__proto__||Object.getPrototypeOf(Reactactor)).call(this,props));_this.state={};var promise=props.wait(props.passthroughProps);if(!isPromise(promise))throw new Error('you must provide an async component');return _this._promise=promise,_this}return _inherits(Reactactor,_Component),_createClass(Reactactor,[{key:'componentWillMount',value:function componentWillMount(){var _this2=this;this._promise.then(function(data){_this2.setState({data:data})}).catch(function(err){throw err})}},{key:'render',value:function render(){return this.state.data?this.state.data:null}}]),Reactactor}(_react.Component);function asyncReactor(component){if(!isFunction(component))throw new Error('you must provide an async component');return function(){var passthroughProps=0<arguments.length&&arguments[0]!==void 0?arguments[0]:{};return(0,_react.createElement)(Reactactor,{wait:component,passthroughProps:passthroughProps})}} | ||
'use strict';var _createClass=function(){function defineProperties(target,props){for(var descriptor,i=0;i<props.length;i++)descriptor=props[i],descriptor.enumerable=descriptor.enumerable||!1,descriptor.configurable=!0,'value'in descriptor&&(descriptor.writable=!0),Object.defineProperty(target,descriptor.key,descriptor)}return function(Constructor,protoProps,staticProps){return protoProps&&defineProperties(Constructor.prototype,protoProps),staticProps&&defineProperties(Constructor,staticProps),Constructor}}();exports.__esModule=!0;exports.asyncReactor=asyncReactor;var _react=require('react');function _classCallCheck(instance,Constructor){if(!(instance instanceof Constructor))throw new TypeError('Cannot call a class as a function')}function _possibleConstructorReturn(self,call){if(!self)throw new ReferenceError('this hasn\'t been initialised - super() hasn\'t been called');return call&&('object'==typeof call||'function'==typeof call)?call:self}function _inherits(subClass,superClass){if('function'!=typeof superClass&&null!==superClass)throw new TypeError('Super expression must either be null or a function, not '+typeof superClass);subClass.prototype=Object.create(superClass&&superClass.prototype,{constructor:{value:subClass,enumerable:!1,writable:!0,configurable:!0}}),superClass&&(Object.setPrototypeOf?Object.setPrototypeOf(subClass,superClass):subClass.__proto__=superClass)}function isFunction(p){return'function'==typeof p}function isPromise(){var p=0<arguments.length&&arguments[0]!==void 0?arguments[0]:{};return isFunction(p.then)}function defer(fn){setTimeout(fn,0)}var Reactor=function(_Component){function Reactor(props){_classCallCheck(this,Reactor);var _this=_possibleConstructorReturn(this,(Reactor.__proto__||Object.getPrototypeOf(Reactor)).call(this,props));return _this.state={},_this}return _inherits(Reactor,_Component),_createClass(Reactor,[{key:'componentWillMount',value:function componentWillMount(){var _this2=this;defer(function(){var promise=_this2.props.wait(_this2.props.passthroughProps);if(!isPromise(promise))throw new Error('You must provide an async component');promise.then(function(data){_this2.setState({data:data})}).catch(function(err){throw err})})}},{key:'render',value:function render(){return'data'in this.state?this.state.data:(0,_react.createElement)(this.props.loader)}}]),Reactor}(_react.Component);function asyncReactor(component){var loaderComponent=1<arguments.length&&arguments[1]!==void 0?arguments[1]:'div';if(!isFunction(component))throw new Error('You must provide an async component, '+JSON.stringify(component)+' given');return function(){var passthroughProps=0<arguments.length&&arguments[0]!==void 0?arguments[0]:{};return(0,_react.createElement)(Reactor,{wait:component,loader:loaderComponent,passthroughProps:passthroughProps})}} |
{ | ||
"name": "async-reactor", | ||
"version": "1.0.3", | ||
"version": "1.0.4", | ||
"keywords": [ | ||
@@ -43,3 +43,3 @@ "react", | ||
"enzyme": "^2.8.0", | ||
"eslint": "^0.11.0", | ||
"eslint": "^3.13.1", | ||
"eslint-plugin-flowtype": "^2.30.4", | ||
@@ -46,0 +46,0 @@ "eslint-plugin-flowtype-errors": "^3.0.3", |
@@ -11,9 +11,28 @@ # async-reactor | ||
## Examples | ||
## Usage | ||
component.js: | ||
```js | ||
asyncReactor(component: Function, loader?: Component): Component | ||
``` | ||
| name | type | description | | ||
|--------------------|------------------|-------------------------------------------------| | ||
| component | Function (async) | The `async` component you want to render | | ||
| loader (optionnal) | Component | Will be shown until the first component renders | | ||
The returned value is a regular `Component`. | ||
### Example | ||
```js | ||
import React from 'react'; | ||
import {asyncReactor} from 'async-reactor'; | ||
function Loader() { | ||
return ( | ||
<h2>Loading ...</h2> | ||
); | ||
} | ||
async function AsyncPosts() { | ||
@@ -30,18 +49,7 @@ const data = await fetch('https://jsonplaceholder.typicode.com/posts'); | ||
export default asyncReactor(AsyncPosts); | ||
export default asyncReactor(AsyncPosts, Loader); | ||
``` | ||
index.js: | ||
```js | ||
import React from 'react'; | ||
import ReactDOM from 'react-dom'; | ||
This example exports a regular React component. You can integrate it into an existing application or render it on the page. | ||
import App from './component'; | ||
ReactDOM.render( | ||
<App />, | ||
document.getElementById('root') | ||
); | ||
``` | ||
See more examples [here](https://github.com/xtuc/async-reactor-examples) | ||
See more examples [here](https://github.com/xtuc/async-reactor/tree/master/examples) |
Sorry, the diff of this file is not supported yet
Empty package
Supply chain riskPackage does not contain any code. It may be removed, is name squatting, or the result of a faulty package publish.
Found 1 instance in 1 package
97813
7
63
54
0