Parameters

Parameters

Parameters

Parameters

Parameters

DoksJS routes can have parameters. Parameters are variables that are passed to the route's component as props.

DoksJS routes can have parameters. Parameters are variables that are passed to the route's component as props.

DoksJS routes can have parameters. Parameters are variables that are passed to the route's component as props.

3

3

min read

min read

Mitchell Bernstein

Mitchell Bernstein

3

min read

Mitchell Bernstein

To define a parameter, you need to use the : character before the parameter name. For example, the following route defines a route that takes a name parameter:


const userRoute = {
  path: '/users/:name',
  component: UserComponent,
};


This route specifies that the user page is located at the /users/:name path and that the UserComponent component will be used to render the page. The :name parameter will be passed to the UserComponent component as a prop.

To use a route with a parameter, you need to pass the value of the parameter to the route's path. For example, the following code will navigate to the user page for the user with the name "John Doe":


const router = new Router();
router.navigate('/users/John Doe');


Here are some additional tips for using parameters in DoksJS:

  • You can use the params property of the route's component to access the parameters that were passed to the route.

  • You can use the matchPath() method to check if a path matches a route with parameters.

  • You can use the redirect property to redirect the user to a different route when a route is navigated to with invalid parameters.