Comments (14)
You probably need to flatten your array and extract the segments you need. Can you show me your route definitions?
from router5.
const routes = [
{
name: 'shop',
path: '/shop',
onActivate: (dispatch) => (params) => {
dispatch(loadProducts(params));
},
children: [
{
name: 'category',
path: '/category/:id',
onActivate: (dispatch) => (params) => {
dispatch(filterProducts(params.id));
}
}
]
},
{
name: '404',
path: '/404'
},
{
name: 'page',
path: '/*path',
onActivate: (dispatch) => (params) => {
dispatch(loadPage(params));
}
}
];
from router5.
I know that Router5 concats names, and while I can write the concat behavior in my plugin, that seems like it would be hard to keep in sync.
I'd much rather ask R5 for a list of matching routes, given the transition-path
.
from router5.
So you want all active segments? all newly activated segments? Keep in mind R5 does not know about your onActivate
handlers.
from router5.
Right. If a child route is activating, I want to include it. (All active segments, regardless of whether they're nested or not. R5, as I see it, should be responsible for parsing and including child route definitions, not userland code.)
This feature is ideally a userland implementation, but since R5 concats child routes (shop.category
), it might make sense to have an API where I could ask the Router instance about matches. So:
let matches = router.match(['shop', 'shop.category']) // args are the output of transitionPath.
matches = [
{
// route definitions for all cases where name matches
name: 'shop.category',
onActivate: () => {}
}
];
I would've added the .
when defining definitions, but that errored when the router initialized.
Does this make sense? For now, I can hack out a solution, but I think an official API would be handy.
from router5.
Currently r5 stores name and path for each route. It has a registry of canActivate
functions separately. onActivate
is different, it doesn't belong to the router, as you said it is userland. r5 only cares about making sure a transition can happen, it doesn't care about what needs to happen on a route change.
I have a repo here: https://github.com/router5/helpers where I can add helper functions to manipulate a manifest of nested routes.
Since R5 concats child routes
It doesn't. When you use transitionPath, it resolves full node names: https://github.com/router5/transition-path/blob/master/modules/index.js#L1-L5
I would've added the
.
when defining definitions, but that errored when the router initialized
That is expected behaviour (from the router perspective). If you add a route like so:
const routes = [
{
name: 'shop.category',
path: '/category/:id',
}
R5 will look for the 'shop' node and if found it will add 'category'. So if you do:
const routes = [
{
name: 'shop',
path: '/shop',
children: [
{
name: 'shop.category',
path: '/category/:id',
}
]
}
It will try to find the node 'shop' from the node 'shop' to add 'category', hence the error
from router5.
Should I be using the children
array when defining routes, or just skip it? If R5's nested states are based on shop.category
style notation, it seems clearest to ignore the nested array if possible.
from router5.
It is really up to you. Nested children attributes avoid you to repeat parent segments, which can be a benefit if you nest deeply. It can be a bit less maintainable if you reorganise your routes quite often.
from router5.
That makes sense. I think that, for now, I won't nest children — just since it keeps the routes array shallow and simpler to maintain.
Thanks for all of your help!
from router5.
👍
from router5.
I'm closing this for now.
from router5.
@nicholaswyoung version >= 2.0.0 registers canActivate
functions for you, and middleware syntax has slightly changed router => (toState, fromState[, done]) => ...
. With redux, you get your store (router => (store, toState, ...) => ...
from router5.
@troch So there is no need for custom Redux middleware? Because if I can retrieve the store in canActivate
router middleware, then it might make sense to drop the onActivate
handlers.
from router5.
store was always available in canActivate
, the novelty is that it is available in middleware functions.
You should not perform data loading in canActivate
, because you cannot be sure at that point the transition can happen (a child segment canActivate
method could still fail it). So middleware (router5 or redux) executing onActivate
handlers is recommended.
from router5.
Related Issues (20)
- router5-plugin-browser base is case-sensitive
- TypeError of router5-transition-path HOT 2
- Unhandled promise rejection Error: Alias is already defined in route node in legacy Edge
- Integration(solid-js): reactivity of instance methods, getState vs subscribe's route, etc
- GitHub Pages and router5(+react) HOT 1
- Is non-latin letters are supported in the "path"?
- Remove the routes HOT 1
- Are arrays allowed as route params? HOT 1
- Improve type definitions
- Listeners plugin exposes incorrect typedefs HOT 1
- How to dynamically change base path on browserPlugin
- roadmap? HOT 2
- How can can pass query params to Link by react-router5? HOT 1
- Feature request on Navigation HOT 5
- BUG: errors handling unicode emoji
- QUESTION: Can I use the browser plugin but have each route specify when to use pushState vs replaceState? HOT 2
- Is this project abandoned? HOT 1
- There is an error in the areStatesEqual method
- Anyone interested to keep the package maintained? HOT 1
- cloneRouter() uses same root node
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from router5.