3. Pages & Routes
In the default main.wasp file created by wasp new, there is a page and a route declaration:
- JavaScript
- TypeScript
routeRootRoute{path: "/",to: MainPage}
pageMainPage{
// We specify that the React implementation of the page is the default export
// of `src/client/MainPage.jsx`. This statement uses standard JS import syntax.
// Use `@client` to reference files inside the `src/client` folder.
component: importMainfrom"@client/MainPage.jsx"
}
routeRootRoute{path: "/",to: MainPage}
pageMainPage{
// We specify that the React implementation of the page is the default export
// of `src/client/MainPage.tsx`. This statement uses standard JS import syntax.
// Use `@client` to reference files inside the `src/client` folder.
component: importMainfrom"@client/MainPage.tsx"
}
Together, these declarations tell Wasp that when a user navigates to /, it should render the default export from src/client/MainPage.
The MainPage Componentβ
Let's take a look at the React component referenced by the page declaration:
- JavaScript
- TypeScript
importwaspLogofrom'./waspLogo.png'
import'./Main.css'
constMainPage=()=>{
// ...
}
exportdefaultMainPage
importwaspLogofrom'./waspLogo.png'
import'./Main.css'
constMainPage=()=>{
// ...
}
exportdefaultMainPage
Since Wasp uses React for the frontend, this is a normal functional React component. It also uses the CSS and logo image that are located next to it in the src/client folder.
That is all the code you need! Wasp takes care of everything else necessary to define, build, and run the web app.
wasp start automatically picks up the changes you make and restarts the app, so keep it running in the background.
Adding a Second Pageβ
To add more pages, you can create another set of page and route declarations. You can even add parameters to the URL path, using the same syntax as React Router. Let's test this out by adding a new page:
- JavaScript
- TypeScript
routeHelloRoute{path: "/hello/:name",to: HelloPage}
pageHelloPage{
component: importHellofrom"@client/HelloPage.jsx"
}
routeHelloRoute{path: "/hello/:name",to: HelloPage}
pageHelloPage{
component: importHellofrom"@client/HelloPage.tsx"
}
When a user visits /hello/their-name, Wasp will render the component exported from src/client/HelloPage and pass the URL parameter the same way as in React Router:
- JavaScript
- TypeScript
constHelloPage=(props)=>{
return<div>Here's {props.match.params.name}!</div>
}
exportdefaultHelloPage
import{RouteComponentProps}from'react-router-dom'
constHelloPage=(props:RouteComponentProps<{ name:string}>)=>{
return<div>Here's {props.match.params.name}!</div>
}
exportdefaultHelloPage
Now you can visit /hello/johnny and see "Here's johnny!"
Cleaning Upβ
Let's prepare for building the Todo app by cleaning up the project and removing files and code we won't need. Start by deleting Main.css, waspLogo.png, and HelloPage.tsx that we just created in the src/client/ folder.
Since we deleted HelloPage.tsx, we also need to remember to remove the route and page declarations we wrote for it. Your Wasp file should now look like this:
- JavaScript
- TypeScript
appTodoApp{
wasp: {
version: "^0.11.0"
},
title: "Todo app"
}
routeRootRoute{path: "/",to: MainPage}
pageMainPage{
component: importMainfrom"@client/MainPage.jsx"
}
appTodoApp{
wasp: {
version: "^0.11.0"
},
title: "Todo app"
}
routeRootRoute{path: "/",to: MainPage}
pageMainPage{
component: importMainfrom"@client/MainPage.tsx"
}
Next, we'll remove most of the code from the MainPage component:
- JavaScript
- TypeScript
constMainPage=()=>{
return<div>Hello world!</div>
}
exportdefaultMainPage
constMainPage=()=>{
return<div>Hello world!</div>
}
exportdefaultMainPage
At this point, the main page should look like this:
Todo App - Hello WorldIn the next section, we'll start implementing some features of the Todo app!