2

I am trying to define a react route that can contain multiple different options.

I am trying to avoid defining a route for each, like below:

<Route  path='/option1' component={Testing}/>
<Route  path='/option2' component={Testing}/>
<Route  path='/option3' component={Testing}/>

In Express, I am able to do this using app.get('/:name(option1|option2|option3)?', (req, res) => {} and then access this data via req.params.name

Also, is there a way that i can pass the path parameter (i.e option1), into my component via props? How would i do this?

Hysii
  • 702
  • 2
  • 10
  • 23
  • 1
    Possible duplicate of [Multiple path names for a same component in React Router](https://stackoverflow.com/questions/40541994/multiple-path-names-for-a-same-component-in-react-router) – Raicuparta Oct 01 '19 at 14:18

2 Answers2

2

You could store an array of paths and map them to the same Component

['/path','/foo','bar'].map(path => <Route key={path} path={path} component={Foo} />)

Is there a way that i can pass the path parameter (i.e option1), into my component via props?

Yes, just use render instead of component

<Route path='/' render={() => <MyComp myProp='foo'/>}

For url parameters use it like this

<Route path='/path/:param' component={Foo} />

And inside Foo.js

console.log(props.match.params.param)

You could also use regexp

<Route exact path="/path/:foo([0-9a-fA-f]{40})" component={Foo} />
Dupocas
  • 20,285
  • 6
  • 38
  • 56
0

paramenters are set with /myroute:myparam and you can retrieve it in target component with this.props.match.params.myparam

Mosè Raguzzini
  • 15,399
  • 1
  • 31
  • 43