Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

API clarifications - use of MobX stores #52

Closed
nareshbhatia opened this issue Dec 10, 2017 · 3 comments · Fixed by #93
Closed

API clarifications - use of MobX stores #52

nareshbhatia opened this issue Dec 10, 2017 · 3 comments · Fixed by #93

Comments

@nareshbhatia
Copy link
Contributor

@kitze and other contributors, thanks for this awesome project! I spent my whole day yesterday switching over a significant application from react-router to mobx-router. I was having subtle data loading issues with react-router, which are now gone by taking the pure state-driven approach by mobx-router.

I'd like to share a couple of experiences when porting over to mobx-router and also a few questions. Hope these will help improve the documentation for future users.

Name of the root store
It was not clear from the docs that mobx-router expects the name store for the MobX store supplied to the Provider. I initially tried to use a more descriptive name like this: <Provider rootStore={rootStore}>, which did not work and was difficult to debug because of obscure error messages.

Usage of root store
I am not entirely sure how the mobx-router uses the store and why it needs the entire root store instead of just its own RouterStore? If the idea was to expose the entire rootStore to it, then why does the goTo() method require a store parameter? mobx-router should be able to use the root store that it knows about.

Creation of the root and router stores
Following the pattern described in MobX documentation, I generally define a root store like this:

class RootStore {
    constructor() {
        this.userStore = new UserStore(this);
        this.todoStore = new TodoStore(this);
    }
}

Extending this example, I think it makes sense to pass the root store to the RouterStore like this:

class RootStore {
    constructor() {
        this.userStore = new UserStore(this);
        this.todoStore = new TodoStore(this);
        this.routerStore = new RouterStore(this);
    }
}

I used the name this.routerStore instead of this.router to clearly make the distinction between the router vs. the router store.

Passing root store to mobx-router
I assume that the official place to hand off the rootStore to the mobx-router is the call to startRouter, is this correct?

startRouter(views, store);

Also IMHO, the first parameter should be called routes - what we are passing in is a map of routes, not views. This will immensely clarify the usage pattern for newbies.

Thanks in advance for your thoughts on these questions and suggestions.

I am really excited to contribute back to this project. @kitze, I see that the last commit to this repo was back in August. I am sure you are busy, but any idea when you are going to be active again? Thanks for this wonderful work!

@thdk
Copy link
Collaborator

thdk commented Apr 13, 2020

I would also change the mobx router api so that the constructor of routerStore accepts the rootStore.
The value provided in the constructor of the rootstore can then be used when calling goTo instead of having to pass the entire rootStore each time we call goTo from somewhere in the application.

Do you still accept PR's for this project?

@kitze
Copy link
Owner

kitze commented Apr 17, 2020

@thdk sure

@thdk
Copy link
Collaborator

thdk commented Apr 18, 2020

@kitze cool, I'll try this out asap

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants