query string react-router path

时光毁灭记忆、已成空白 提交于 2019-12-22 18:06:24

问题


I am using react-router 3.0.2 and trying to configure router path with query string. This is how I have configured my router:

<Router history={browserHistory}>
                <Route path="abc/login.do" component={LoginComponent}/>
                <Route path="abc/publicLoginID.do?phase=def" component={VerifyComponent} />
                <Route path="abc/publicLoginID.do?phase=ghi" component={ImageComponent}/>
                <Route path="*" component={LoginComponent}/>
</Router>

I understand this is not the right way to specify query string (?) in "Route". How do I make sure that whenever a user enters "http://localhost:3000/abc/publicLoginID.do?phase=def" in the url, "VerifyComponent" shows up, and when he enters "http://localhost:3000/abc/publicLoginID.do?phase=ghi" in the url, "ImageComponent" shows up.

I did check some cases here: react-router match query string and Querystring in react-router, but unable to figure out how to make it work.


回答1:


You can write a wrapper component, that will switch what to render based on the query parameter

<Router history={browserHistory}>
   <Route path="abc/login.do" component={LoginComponent}/>
   <Route path="abc/publicLoginID.do" component={WrapperComponent} />
   <Route path="*" component={LoginComponent}/>
</Router>

//WrapperComponent

WrapperComponent = (props) => {
   if (props.location.query.phase==="def"){return <VerifyComponent {...props}/>}
   if (props.location.query.phase==="ghi"){return <ImageComponent {...props}/>}
}


来源:https://stackoverflow.com/questions/42123743/query-string-react-router-path

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!