Best way to reference deep nested props passed through Provider?
Hi, I'm newbie with mobx and I'm trying to refactor your excellent Contact List Demo Project to use Provider and react-router.
I'm having problems to use deep nested props in a elegant way inside a React Component.
These are some snippets from the code
app.jsx
....
const tagStore = new TagStore();
const contactStore = new ContactStore(tagStore);
const viewState = new ViewState(contactStore, tagStore);
const store = {
tagStore: tagStore,
contactStore: contactStore,
viewState : viewState
};
ReactDOM.render(
<Provider store={store} >
<Router history={browserHistory} routes={routes} />
</Provider>,
document.getElementById('app')
);
routes.js
....
export default (
<Route path="/" component={Main}>
<IndexRoute component={Home} />
<Route path="contact/:contactID" component={ContactView} />
<Route path="tag/:tagID" component={TagView} />
</Route>
);
view-state.js
....
export class ViewState {
@observable selection = null;
...
}
contact-view.jsx
....
@observer(["store"])
class ContactView extends React.Component {
@observable firstNameValue;
@observable lastNameValue;
@observable tagId = 0; // AutoComplete has no reset api, abuse react keys...
contact;
//@observable contact; not allowed in strict mode
//contact2; Created with extend observable
contact3;
componentWillMount() {
//Trying different ways to point to a nested prop... Only #3 seems to work
//1.- Direct way...
this.contact = this.props.store.viewState.selection;
//2.- Using extendObservable
extendObservable(this, {contact2: this.props.store.viewState.selection});
//3.- Using autorun...
autorun(() => {this.contact3 = this.props.store.viewState.selection})
this.resetInputValues(this.contact);
}
componentWillReceiveProps(nextProps) {
this.resetInputValues(nextProps.store.viewState.selection);
//nextProps.store.viewState.selection shows always the right data...
}
render() {
console.log("Full path ok", isObservable(this.props.store.viewState.selection), this.props.store.viewState.selection.username);
console.log("this.contact only 1st render ok", isObservable(this.contact), this.contact.username);
console.log("this.contact2 only 1st render ok", isObservable(this.contact2), this.contact2.username);
console.log("autorun way ok", isObservable(this.contact3), this.contact3.username);
...
}
What would be the best way to accomplish this?
- Using the full path in all methods and @actions seems not very readable and efficient
- Using a pointer to the nested prop seems not to refresh (only shows the right data the 1st time, after that it "freezes").
- The same seems to happen when the "pointer" is created with extendObservable.
- Using autorun() seems unnecessary if using @observer and @observable props, isn't it?
Note: isObservable(...) shows true in all 4 cases
Thank you very much for your help! PS: If would be great if somebody more experienced would convert the contact-list demo project to use Provider and react-router showing the best practices to use!!
First a note on your app.jsx:
You can just pass multiple stores with Provider. So <Provider tagStore={tagStore} ... etc />. I would recommend that. Technically there is nothing wrong with your approach, but splitting them up is more declarative; you will be determine later on more easily which stores are used where etc. as your code base grows.
That also makes your paths shorter: this.props.viewState.selection.
If you use variables multiple times in a function, personally I like object destructerings a lot:
const {selection} = this.props.viewState
About your approaches:
- This copies the current selection just one time to your component, during mount. So this will become outdated quicky indeed
- Same thing, you copy it into an observable property, but a new one
- ..which you fix with an autorun. Which works but is kinda ugly, because you are not deriving a value here, but producing a new value using a side effect. This is less tracable in the end and requires more manual labor. The autorun should be disposed in unmount for example.
Your problem could simple be solved by using this on your component:
@computed get contact4() {
return this.props.viewState.selection
}
Or, same thing in the constructor using just ES5:
extendObservable(this, {
contact5: function() {
return this.props.viewState.selection
}
})
Now you can use this.contact4 resp. this.contact5 everywhere you want.
Computed properties are automatically kept in sync, but only when needed / in use by the component.
That being said, I think personally I would just keep the props/stores paths
I plan to update the contact list with Provider soon. Adding react-router as well might be nice indeed.
I hope that helps!
Thank you @mweststrate for so many options and advices to solve it! :) Now it works like a charm!
Great news that your are planning to update the contact list demo project! I'll keep an eye on it.