简体   繁体   English

javascript如何分配参考值?

[英]how javascript assign reference value?

I am learning react and i was using react-router-dom library to change the routes of the project.我正在学习 react,我正在使用react-router-dom库来更改项目的路由。 So i added from the library and added history to it.所以我从库中添加并添加了历史记录。 And added createBrowserHistory from npm package history .并从 npm package history添加了createBrowserHistory

<Router history={createBrowserHistory()}>

But it was not changing the routes and was generating a warning.但它并没有改变路线,而是产生了警告。

You cannot change <Router history>

then i changed it to and then passed in Router然后我把它改成然后传入路由器

const history = createBrowserHistory()
<Router history={history}>

Passing the browser history to a variable worked.将浏览器历史记录传递给变量有效。 Why was this happening?为什么会这样? Is this some concept of Javascript ?这是 Javascript 的一些概念吗?

I'm not sure but I think main problem here could be related to the scope of variable.我不确定,但我认为这里的主要问题可能与变量的范围有关。 While you creating object inside of jsx it will be stored inside of Router component and maybe react needs to have it accessible outside of it.当您在 jsx 内部创建对象时,它将存储在 Router 组件内部,并且可能需要在其外部访问它。 (For example when history changes react knows that it should rerender component) (例如,当历史更改时,react 知道它应该重新渲染组件)

But anyway if you are beginner you don't need to use Router component, check BrowserRouter https://reacttraining.com/react-router/web/api/Route但是无论如何,如果您是初学者,则不需要使用路由器组件,请检查 BrowserRouter https://reacttraining.com/react-router/web/api/Route

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM