[英]How to pass a variable between web pages when using Reactjs Router Switch to navigate
I am working on a web application that can load one of several scenarios based on a selection that a user makes. 我正在开发一个Web应用程序,该应用程序可以根据用户的选择加载几种方案之一。
A webpage has already been implemented to load a scenario, which is initiated by making an API call to the back end (python via flask). 已经实现了一个网页来加载场景,这是通过对后端(通过烧瓶通过python)进行API调用来启动的。
The web-based UI is in javascript making use of react js. 基于Web的UI在JavaScript中使用了react js。
At the moment I use the Router Switch method to allow navigation between a few web pages. 目前,我使用路由器切换方法来允许在几个网页之间导航。
What I would like to achieve is: 我想要实现的是:
renderRedirect()
function, and triggers navigation to the Scenario page. renderRedirect()
函数,并触发导航到“方案”页面。 What I don't know is how to get the information (just a string scenario_id
) from the Home page to the Scenario page. 我不知道如何从主页到“方案”页面获取信息(只是一个字符串
scenario_id
)。
I found this question/answer , but to implement it I would have to rework my current Switch
setup to instead make use of router.push
, unless I am mistaken? 我找到了这个问题/答案 ,但是要实施该问题/答案 ,我必须重新设计当前的
Switch
设置,以改为使用router.push
,除非我弄错了?
Here is my current Routing code: 这是我当前的路由代码:
render((
<BrowserRouter>
<Switch>
<Route exact path='/new-scenario' component={NewScenario}/>
<Route exact path='/about' component={About}/>
<Route exact path='/' component={Home}/>
</Switch>
</BrowserRouter>
), document.getElementById('root'));
If I understood you only want the scenario_id, I think you can do it via url params 如果我了解您只需要cenario_id,我认为您可以通过url参数来实现
if you select scenario_id 1 on home page your url can be something like /scenario/1 如果您在首页上选择censage_id 1,则您的网址可能类似于/ scenario / 1
so you could define your route like this 所以你可以这样定义你的路线
<Route exact path="/scenario/:id" render={props => <ScenarioComponent {...props} /> } />
with this code you're sending the :id part of the url to your component via props you can get the value on Scenario with 使用此代码,您将通过prop将url的:id部分发送到组件,您可以使用以下方法获取Scenario上的值
this.props.match.params.id
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.