Skip to content

Instantly share code, notes, and snippets.

@uditalias
Last active February 2, 2017 19:51
Show Gist options
  • Save uditalias/88248f6e985369fccfa233557f173749 to your computer and use it in GitHub Desktop.
Save uditalias/88248f6e985369fccfa233557f173749 to your computer and use it in GitHub Desktop.
Mobx `inject()` + `observe()` in one comfortable function = `mobxify()`
import React, {Component} from 'react'
import {View, Text} from 'react-native'
import {mobxify} from 'utils'
class App extends Component {
render() {
return (
<View>
<Text>mobxify is awesome!</Text>
</View>
);
}
}
// do:
export default mobxify(["myStore"], App);
// instead of:
// export default inject(["myStore"])(observer(App));
/**
* @providesModule utils
*/
import {observer, inject} from 'mobx-react/native'
export const mobxify = (injectable, Comp) => {
if(typeof injectable !== 'string' && !(injectable instanceof Array)) {
Comp = injectable;
return observer(Comp)
}
return inject(injectable)(observer(Comp));
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment