-
-
Notifications
You must be signed in to change notification settings - Fork 104
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
How to activate nested components with ids from the entire chain of nested routes. #136
Comments
There are plenty ways how you can go. import UniversalRouter from 'universal-router';
const CourseLayout = (props, content) => `
<section class="course">
<h1>${props.title}</h1>
${content}
</section>
<aside class="sidebar">${props.details}</aside>
`;
const ChapterLayout = (props, content) => CourseLayout(props, `
<section class="chapter">
<h2>${props.title}</h2>
${content}
</section>
`);
const LessonLayout = (props, content) => ChapterLayout(props, `
<section class="lesson">
<h3>${props.title}</h3>
${content}
</section>
`);
const router = new UniversalRouter([
{
path: '/:course',
async action({ params }) {
const data = await fetch(`/api/course/${params.course}`);
return CourseLayout(data, `<p>${data.description}</p>`);
}
},
{
path: '/:course/:chapter',
async action({ params }) {
const data = await fetch(`/api/course/${params.course}` +
`?include-chapter=${params.chapter}`);
return ChapterLayout(data, `<p>${data.chapter.description}</p>`);
}
},
{
path: '/:course/:chapter/:lesson',
async action({ params }) {
const data = await fetch(`/api/course/${params.course}` +
`?include-chapter=${params.chapter}` +
`&include-lesson=${params.lesson}`);
return LessonLayout(data, `<p>${data.lesson.description}</p>`);
}
},
]);
router.resolve(location.pathname).then(html => {
document.body.innerHTML = html;
}); this also will work with nested routes: const routes = {
path: '/:course',
children: [
{ path: '', action(context, params) {/* course */} },
{ path: '/:chapter', children: [
{ path: '', action(context, params) {/* chapter */} },
{ path: '/:lesson', action(context, params) {/* lesson */} },
] },
],
}; |
Looks like I completely misunderstood the router architecture. I experimented a bit and I found that actually routes give plenty of control over what happens in the application. Here's a sample I used for experiments.
Accessing
An improvement will be to also prevent new GETs after first load for the parents that don't change ids.
Now I have to figure out how to emulate the |
|
After implementing the demo example I have reached a roadblock. In the intro example the routes seem to have a 1 to 1 relation between route and action.
I am used to building nested components that have a router outlet (Angular 2) or match components (React). For example I would have a nesting like this:
How do I feed the corresponding data for each layer of the app? Currently I am not aware how can I get the ids from parents of nested routes without having to create monolitic routes like these
path: '/:course/:chapter/:lesson'
. I thought that I can just fire actions in the state store that will be received by the components. But than what if I move to another section of the app where I need to tear-down some components and instantiate others. '/blog/:category/:posts'`.The text was updated successfully, but these errors were encountered: