Skip to content

Instantly share code, notes, and snippets.

@mostr
Created October 10, 2016 10:37
Show Gist options
  • Save mostr/e8366c9fb64d23b96f5fc05ce23b572c to your computer and use it in GitHub Desktop.
Save mostr/e8366c9fb64d23b96f5fc05ce23b572c to your computer and use it in GitHub Desktop.
MobX observer that allows passing function selector
import React from 'react';
import ReactDOM from 'react-dom';
import * as mobx from 'mobx-react';
import {state, actions} from './store';
// passing function to observer (with implicit inject) is not supported natively, so make up your own one
// pass either function or plain old selector strings array
// WARNING: doesn't work if "observer" used as decorator
let observer = (selector, Component) => {
let inject = typeof selector === 'function' ? mobx.inject(selector) : mobx.inject(...selector);
return inject(mobx.observer(Component));
};
let selector = (all) => {
return {
todos: all.state.todos,
actions: all.actions
}
};
let App = observer(selector, ({todos, actions}) => {
return <div>Do your app stuff here with "todos" and "actions"</div>
);
ReactDOM.render(
<mobx.Provider state={state} actions={actions}><App/></mobx.Provider>,
document.getElementById('root')
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment