Skip to content

Instantly share code, notes, and snippets.

@valentinvichnal
Forked from theodorDiaconu/methodContainer.js
Created October 31, 2016 14:44
Show Gist options
  • Save valentinvichnal/1a45963ce313de97b1b245d44030557f to your computer and use it in GitHub Desktop.
Save valentinvichnal/1a45963ce313de97b1b245d44030557f to your computer and use it in GitHub Desktop.
React Meteor Method Container
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