user293895
user293895

Reputation: 1527

Generating React/Express Routes in Javascript

Currently I have a number of routes that follow formulas like so:

items/:item?

Is there a library that will allow me to do something like so:

generateUrl('items/:item?', { item : '1' }) = 'items/1'

Upvotes: 0

Views: 24

Answers (1)

user293895
user293895

Reputation: 1527

I have found an answer, the package react-router contains a function here that does the job: https://github.com/ReactTraining/react-router/blob/master/packages/react-router/modules/generatePath.js#L23

So to generate my own routes I have to do the following:

import { generatePath } from "react-router"; generatePath('/items/:item?', { item: itemID });

Make sure you also install the path-to-regexp module.

Upvotes: 1

Related Questions