Project structure
To add app pages to an app, create apages directory within src/app. The pages directory should contain:
- A JSON configuration file that defines the pages configuration (
Pages-hsmeta.jsonis recommended). - A React file that renders the pages component (
Pages.jsxorPages.tsxis recommended). - A
package.jsonfile to handle any needed dependencies.
App pages configuration
In the*-hsmeta.json configuration file for your app pages, include the properties below.
Building the React front-end
The UI of app pages is created by a React component file, either.jsx or .tsx. This file lives in the pages/ directory alongside the pages configuration JSON file (*-hsmeta.json). In the configuration file, you’ll specify the path of the React file in the entrypoint field.
Routes are defined using createPageRouter, which accepts JSX route definitions and returns a React component. For complete examples, see the create app pages guide.
App page URLs
Your app pages can be accessed using the following URL patterns:
For example, following the URL path structure above, if a user who installed your app has a HubSpot account ID (i.e., their
hubId) of 12345 and your app ID was 67890, they could navigate to their home page at the following URL:
React page hooks
App pages provide hooks to access routing information within your page components.usePageRoute
Use theusePageRoute hook to access the current page path and all parameters (both path parameters and query parameters):
usePageRoute hook returns an object with:
Programmatic navigation
You can navigate to app pages programmatically using thenavigateToPage action from the useExtensionActions hook:
navigateToPage function accepts an options object with the following properties:
createPageRouter
ThecreatePageRouter function accepts JSX route definitions and returns a React component that handles routing. Call it at the module level with your <PageRoutes> tree:
Parameters
Returns
A React component (PageRouter) that you render in your app to activate routing.
Page routing components
ThePageRoutes component and its sub-components are used with createPageRouter to define routing for app pages:
PageRoutes: Container for route definitions. SupportslayoutComponentfor wrapping child routes with shared UI.PageRoutes.IndexRoute: Defines the home page (index route) that renders at the root URL of your app.PageRoutes.Route: Defines a named page route with apathandcomponent.PageRoutes.AnyRoute: Defines a catch-all route for unmatched paths, useful for custom 404 pages.
id prop. When a route matches, the id value is available as routeId from usePageRoute(). See the route IDs section in the routing guide for details.
For detailed documentation on routing components including nested routes, path parameters, and wildcard routes, see:
Manage dependencies
You can include dependencies for your app pages in apackage.json file within the pages/ directory. By default, when adding app pages through the hs project add command, a package.json file will be created for you with the following dependencies:
@hubspot/ui-extensionsreacttypescript
package.json file, you can run the hs project install-deps command in your project directory.