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
Make params object observable and make installable from github with jspm #1
Conversation
Hey @salfield thanks for that! |
@salfield I've taken some time to review this. Your Also as specified in the CONTRIBUTING.md this repo uses semantic release so the commit messages should respect that convention. Ultimately, wouldn't a simple
|
Verify that route params are observable
Check that route.params is an observable
@salfield I've written a test about the issue (are You can checkout the branch dev and run It seems to me that the plugin works as expected and the route.params is an observable and so an Am I missing something? |
@LeonardoGentile apologies for not following the contributor guidelines - it was just a very quick customisation for my purposes and given the size of the package I'm happy to maintain my fork.
I guess you would expect that the parent component would re-render, re-rendering the child. However it doesn't, as I'm doing a componentShouldUpdate check based on intersectionNode (similar to your own) in RouteNode. I pass route.params and route.name down from my RouteNode into the child component. Maybe I wouldn't have this problem if I used your react-mobx-router5 package, however I didn't understand the intended way that it should be used - nor the intention behind some parts of the implementation. Your test dereferences the routerStore in the autorun with: Its true that if we put the suggested @compute params function on the routerStore, it would be possible to use the params like routerStore.params, and the would update when changing. However, how would you then differentiate the params on route v's transitionRoute, previousRoute etc. |
I see your point now, although there is a bit of confusion because this has nothing to do with the components of react-mobx-router5 which is a separate package. I have tried to build on top of your solution but I'm not really convinced because all edge cases should be covered. Rewriting a bit of your function
Basically we say: A) if B) if Even tough this might seems logic, whenever the
but then it complicates things because instead of simply checking against null we would check against an empty object having an empty params...I wouldn't do that. Do you agree? About you problem, I was thinking: using router5 the only way to change params is to navigate to a new (could be the same) route and passing the required params (unless I'm mistaken). So if you think about it each param change is always triggered by a route change. So your initial concern about observing route instead of param shouldn't really matter. Just obser the route and the params should always be in sync. So using my initial implementation why don't you just pass down the route observable and use the params inside the component?
And if you want to inject extra params (why would you?) just do it in |
Instead of substitute the old observable route for each on each route transaction now the routes are updated starting from a basic empty route object. This object has already an empty observable map property. Might close #1
Ultimately take a look at this possible implementation that would satisfy both of our needs, what do you think? https://github.com/LeonardoGentile/mobx-router5/blob/observable-params/src/modules/RouterStore.js |
@LeonardoGentile yes, I really like the use of emptyRoute to avoid the null problem you mentioned earlier. This looks much better to me. With regard to when you might want to add new params - I'm thinking for example when the params represent url query parameters which appear to be supported by router5. http://router5.github.io/docs/path-syntax.html It seems to be to be quite likely that when using query parameters that you might want to react to the adding and removal of parameters, whilst the "route.name" itself has not changed. |
@salfield I've completed a final implementation of this but I decided I won't support it for now, feel free to grab the code at: https://github.com/LeonardoGentile/mobx-router5/blob/observable-params/src/modules/RouterStore.js I'm explaining my decision: any state change from router5 should be considered a new route. This should be considered immutable from the standpoint of mobx because only router5 should be allowed to modify and emit a new state. Furthermore all the internals of a state (path, params, meta) will never change unless router 5 emit a new state. So I strongly think we should observe the |
As per discussion on #1 a route should only be changed and emitted by router5. So I now cosiders the routes exposed by the store are immutable, better to use a reference to the origina object. BREAKING CHANGE: the whole objects routes: route, previousRoute and transitionRoute are now only observable reference to the original objects emitted by router5
Hi Leonardo,
Thanks, for the module, I think there is quite a nice fit between mobx and router5 state driven routing - so started using this.
Not sure if this interesting to you, but I made the parameters into an observable map. I was trying to 'observe' params.x and expected that since params was an observable object that it would change on update. It doesn't because it was completely replaced. So I turned it into an observable map.
Let me know if you are interested in merging, or if I should carry on with my fork?
Many thanks,
Tom