Adding navigation
This step consists in transforming a standard React application into a onekijs
application and adding the possibility to navigate between pages
Final result
The result of this step is as follows:
You can now click on a product (e.g the "Phone XL" link) to view the product details
.
Please note that the URL changes when you click on the link.
- Vite App
- Nextjs App
Upgrading to an onekijs
application
In order to be able to navigate between pages, we first need to transform the basic react application created in the previous step into an onekijs
application.
First add onekijs
to your project
yarn add onekijs
- Vite App
- Nextjs App
<App/>
component.loading...
<App />
is the main component that primes the onekijs
framework
This component is usually the most external component of an application and is responsible of booting the following components:
Component | Description |
---|---|
Router | By default, it starts a BrowserRouter, but this can be configured. The router is responsible for managing the navigation between pages *The router is used in this step.*** |
Global state management | It starts a Redux store to manage global state. The redux store will be used in the next step. |
Others | It starts other components described later on |
Adding the routes
We need to declare the routes so React router can link a Page component
to a URL.
The routes are declared in their own file named _router.tsx
and can be nested.
For example:
- Top level routes like
/
,/users
,/products
, ... are defined in src/pages/_router.tsx - Sub routes like
/products/new
,/products/:id
,/products/:id/edit
, ... are defined in src/pages/products/_router.tsx
loading...
loading...
useRouteMatch is a Hook. Hooks are a new addition in React 16.8. They let you use state and other React features without writing a class
onekijs
is heavily based on hooks. Therefore, it's important to understand this concept
<NextApp/>
component.loading...
<NextApp />
is the main component that primes the onekijs
framework
This component is usually the most external component of an application and is responsible of booting the following components:
Component | Description |
---|---|
Router | Standard Next.js router. The router is responsible for managing the navigation between pages The router is used in this step. |
Global state management | It starts a Redux store to manage global state. The redux store will be used in the next step. |
Others | It starts other components described later on |
Adding navigation
To navigate between pages, onekijs
provides the component <Link/>
which replaces the standard HTML tag <a/>
The <Product />
component that displays a product in the product list is updated to use <Link />
const Product: FC<ProductOptions> = ({ product, id, onClick, onNotify }) => {
return (
<div>
<h3>
<Link href={`/products/${id}`}>{product.name}</Link>
</h3>
{product.description && <p>Description: {product.description}</p>}
<button onClick={onClick}>Share</button>
{product.price > 700 && (
<p>
<button onClick={onNotify}>Notify me</button>
</p>
)}
</div>
);
};
The navigation between pages is done entirely on the browser side. There is no call to a web server
Next step
In the next step, we will add a "shopping cart" page that displays the list of products that the user wants to buy. This feature will allow to introduce the global state of an application.