LeonardoGentile / mobx-router5

Router5 integration with mobx
MIT License
22 stars 7 forks source link

Router Store has a null state #4

Closed optimatex closed 6 years ago

optimatex commented 6 years ago

Hi. I have followed all examples, provided in docs, but my router appears with null state. Here is my implementation:

import { createRouter } from 'router5';
import browserPlugin from 'router5/plugins/browser';
import loggerPlugin from 'router5/plugins/logger';
import { mobxPlugin, RouterStore } from 'mobx-router5';

const routerStore = new RouterStore();

const routes = [
  { name: 'signup', path: '/signup' },
  { name: 'user', path: '/user/:uid' },
];

export default function configureRouter(useLoggerPlugin = false) {
  const router = createRouter(routes, { defaultRoute: 'home' })
    .usePlugin(mobxPlugin(routerStore)) // Important: pass the store to the plugin!
    .usePlugin(browserPlugin({}));

  if (useLoggerPlugin) {
    router.usePlugin(loggerPlugin);
  }
  console.log('%c 1router', 'color: #ffcb14', router);
  return { router, routerStore };
}

as the results, my router store contains null in route props

LeonardoGentile commented 6 years ago

Hello, You don't have home route in your routes, have you tried to change the defaultRoute?

optimatex commented 6 years ago

@LeonardoGentile I have provided concise routes array, in my current code there is the home route. I have tried to tweak this parameter but it doesn't help me

LeonardoGentile commented 6 years ago

Ok, but did you start your router? Do you actually navigate from one route to another as described in router5 docs?

In case you couldn't solve it, please provide a code sample so that I might be able to help you

LeonardoGentile commented 6 years ago

Also, is there any piece of code observing the @observable variables exposed by the store? Try with a console log inside a mobx autorun

optimatex commented 6 years ago

@LeonardoGentile thanks for the help. I put all my relevant code here https://codesandbox.io/s/jlxopz1lk5

LeonardoGentile commented 6 years ago

As I suspected you don't start your router, please do something like this:

// Renders the entire app when the router starts
router.start((err, state) => {
    render(<ObserverApp />, document.getElementById("root"));
});

The react app should start after the router is started. Btw if you are using this with react I suggest you to pair this with my other package: react-mobx-router5

optimatex commented 6 years ago

oh, sorry. I suppose I did miss this in docs, however, I read almost all pages from here http://router5.github.io/docs. Maybe it can be usefull to others to emphasize this part. Anyway, thanks for resolving my issue. And for the cool lib. I'll try react-mobx-router5 too

LeonardoGentile commented 6 years ago

No problem, cheers!