Coder Social home page Coder Social logo

Comments (14)

troch avatar troch commented on May 3, 2024

You probably need to flatten your array and extract the segments you need. Can you show me your route definitions?

from router5.

secretfader avatar secretfader commented on May 3, 2024
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.

secretfader avatar secretfader commented on May 3, 2024

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.

troch avatar troch commented on May 3, 2024

So you want all active segments? all newly activated segments? Keep in mind R5 does not know about your onActivate handlers.

from router5.

secretfader avatar secretfader commented on May 3, 2024

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.

troch avatar troch commented on May 3, 2024

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.

secretfader avatar secretfader commented on May 3, 2024

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.

troch avatar troch commented on May 3, 2024

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.

secretfader avatar secretfader commented on May 3, 2024

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.

troch avatar troch commented on May 3, 2024

👍

from router5.

troch avatar troch commented on May 3, 2024

I'm closing this for now.

from router5.

troch avatar troch commented on May 3, 2024

@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.

secretfader avatar secretfader commented on May 3, 2024

@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.

troch avatar troch commented on May 3, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.