-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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 Implement Routing and Navigation" - is this still correct? #748
Comments
Good catch! I will update the docs. In meanwhile, here is the outline how routing works (with the difference that RSK supports nested routes) - You might not need React Router on Medium.com |
Thanks for the speedy response. I have learned more after I wrote the original post. I found a critical sentence in the Getting Started page for universal-router at https://github.com/kriasoft/universal-router/blob/master/docs/getting-started.md:
So "all I need to do" in RSK is have each page/component's index.js page export a default object with a path, action, and optional children property. If resolve() matches on the path (or path+child), action() gets called and returns a string, including the expansion of React templates. I think... is this true? |
NB The example just below the cited paragraph has an error, I think. The example should render |
@richb-hanover there are two important notions about var routes = [
{ path: '/', action: () => 'home page' },
{ path: '/hello', action: () => 123 }
];
var result1 = await Router.resolve(routes, { path: '/' }); // resolve() returns a Promise
// result1 => "home page"
var result2 = await Router.resolve(routes, { path: '/hello' }); // resolve() returns a Promise
// result2 => 123 The way RSK is currently configured, it uses this convention that route handlers (actions) should return React components. E.g. Since route handlers are not very concise, it might be a good idea to split routes into multiple files: const routes = [
require('./routes/home'),
require('./routes/tasks'),
]; This way you can have React component and routing information collocated within the same folder. ... You can also use another convention if you like. For example, put all routes into the same file. Or, make the route handlers return more data, e.g.: {
path: '/tasks/:id',
async action({ params }) {
const resp = await fetch(`/api/tasks/${params.id}`);
const data = await resp.json();
return { title: `To-Do (${data.length})`, component: TodoList, props: data };
}
} or, pass const routes = [
{
path: '/hello',
action({ render }) {
return render(<HelloPage />);
}
}
];
Router.resolve(routes, { path: '/hello': render: ... }); |
That is useful information for understanding the current state of RSK. A follow-on question: I want to have an /api module that has several sub-paths (/api/AAA, /api/BBB/, /api/CCC, etc.) In RSK 0.5.1, I previously had a separate file with a number of router.get() calls. It sounds as if this should become another file ( src/routes/api/api.js) that exports its own default object for each of the paths. Some of the responses to the /api calls will be JSON, some will be bare text, some might be HTML. How (in the RSK framework) do I get a handle to the response to send the data back? Thanks! |
@richb-hanover here is pattern that you may want to use, for each RESTful API endpoint create a separate file inside the
|
Shouldn't the final |
@richb-hanover yep, right. |
I issued a PR for a placeholder page to update the How to Implement Routing page: |
A quick note. Really like the "Why this is better" note in the PR. Speaking for myself, I am still learning a lot about the React ecosystem. Having a few lines explaining what the role of the code is and why it was done that way is a huge help to put things in context. A small section focused on that as people do the docs is very appreciated. |
@shekaryenagandula seems like that you have typo. In your src\routes\hellopage\index.js you tried to import HomePage, but in your folder you don't have it. I suppose you should import HelloPage instead |
@richb-hanover thank you very much for crating this issue! Unfortunately, we have close it due to inactivity. Feel free to re-open it or join our Discord channel for discussion. NOTE: The |
The "How to Implement Routing and Navigation" page at https://github.com/kriasoft/react-starter-kit/blob/master/docs/recipes/how-to-implement-routing.md appears to describe an earlier (non-universal-router) version of ReactStarterKit.
According to CHANGELOG.md, the components moved to src/routes, while the page above still refers to them in src/components. It appears that all the signatures of the functions/components changed, as well.
The project might use universal-router, but the last sentence of the API doc ("Sorry, the rest of the guide is currently under development. Come back soon.") doesn't give a strong sense of confidence.
The text was updated successfully, but these errors were encountered: