preactjs / preact-router

:earth_americas: URL router for Preact.

Home Page:http://npm.im/preact-router

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

path property: how to make url param a number vs a string?

nolatone opened this issue · comments

Given this code snipped:

<Router> <SomeComponent path="/api/v2/:jobId/:optional?/:params?" /> </Router>

The jobId from the source is an integer, the property inside SomeComponent is a string.

I tried using a regex to convert it to an integer like this: ":jobId(\d+)" bu that did not have any effect.

How can I set a path parameter to the desired type in preact-router?

Thanks,

Paul

Sorry, are you asking how to enforce integers as that path segment for the component as in differentiating /api/v2/5 and /api/v2/foo, or asking how make the prop within the component an integer?

If it's the former, you'll need a shell component that checks the path segment type and renders component A if it's an number or component B if it's not.

If it's the latter, you can't. URLs and their path segments are strings by definition. You can handle conversion yourself by casting the prop to a number (const jobId = Number(props.jobId)), in which case you can also handle the situation in which it's not a number.

Hope this helps.