$0.02

how to set React.useState based on URL

Ask a question+
0

Hi I am trying to set the following to different numbers based on the page the user is on using window.location.path

const [value, setValue] = React.useState(1);

I have tried if statement and I seem to get errors so my thinking was to set a params to

<Route exact path="/" component={Home} />

however while I understand I could do something like

<Route exact path="/:id" component={Home} />

the issue is the following as you can see below I have a number of set paths, the tabs in the nav bar are linked to the main path (/, /about, /news, /programs etc)

<Switch>
           <Route exact path="/" component={Home} />
           <Route path="/about/" component={About} />
           <Route path="/news/:id" component={News} />
           <Route path="/programs/:id" component={Programs} />
           <Route path="/podcast/:id" component={Podcast} />


</Switch>

I want to be able to send a number with each Route so that way I can just set the following with something like

const [value, setValue] = React.useState({pageID});

I was thinking something like

<Route path="/podcast/:id" pageID="4" component={Podcast} />

Could someone please help point me in the right direction on how to do this.

add comment

0 Answer

Your Answer