Created
August 31, 2016 14:54
-
-
Save theodorDiaconu/80c5aec8f70886dbbd386039a56ab161 to your computer and use it in GitHub Desktop.
React Meteor Method Container
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from 'react'; | |
/** | |
* Method container for React. Very helpful to avoid boilerplate code. | |
* | |
* export default methodContainer((props) => { method, param }, Component) | |
* Will send {error, data, ready} as props to the specified Component | |
* | |
* On behalf of www.drivindu.com | |
* | |
* @param callDataFn | |
* @param component | |
* @returns {*} | |
*/ | |
export default (callDataFn, component) => { | |
return class extends React.Component { | |
constructor() { | |
super(); | |
this.state = { | |
ready: false | |
} | |
} | |
componentWillMount() { | |
const callData = callDataFn(this.props); | |
Meteor.call(callData.method, callData.param, (error, data) => { | |
this.setState({ | |
error, | |
data, | |
ready: true | |
}) | |
}) | |
} | |
render() { | |
return React.createElement(component, this.state) | |
} | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment