-
-
Save valentinvichnal/1a45963ce313de97b1b245d44030557f to your computer and use it in GitHub Desktop.
React Meteor Method Container
This file contains hidden or 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