简体   繁体   English

单击后退按钮时,react-router意外执行

[英]react-router performs unexpectedly when clicking back button

i'm new to react,and this is just a part of my code. 我是新来的人,这只是我的代码的一部分。

About.js About.js

import React from 'react'

export default React.createClass({
    render() {
        return <div > About < /div>
    },
    componentDidMount() {
        var elem = document.createElement("div");
        elem.innerText = "okokokkokoko";
        document.getElementById('app').appendChild(elem);
    }
})

App.js App.js

import React from 'react'
import { Link } from 'react-router'

export default React.createClass({
  render() {
    return (
      <div>
        <h1>React Router Tutorial</h1>
        <ul role="nav">
          <li><Link to="/about">About</Link></li>
          <li><Link to="/repos">Repos</Link></li>
        </ul>
      </div>
    )
  }
})

index.js index.js

import React from 'react'
import { render } from 'react-dom'
import { Router, Route, browserHistory } from 'react-router'
import App from './modules/App'
import About from './modules/About'
import Repos from './modules/Repos'

render((
  <Router history={browserHistory}>
    <Route path="/" component={App}/>
    <Route path="/repos" component={Repos}/>
    <Route path="/about" component={About}/>
  </Router>
), document.getElementById('app'))

when i click the browser's back button on the About page,borwser will be back to the App page,but the page still retains the element 当我单击“关于”页面上的浏览器的后退按钮时,浏览器将返回到“应用程序”页面,但该页面仍保留该元素

<div>okokokkokoko</div>

how can i solve this problem without changing the code of About.js? 我如何在不更改About.js代码的情况下解决此问题?

You need to understand React first. 您需要首先了解React。 You can't create elements like this since React is unable to track them. 您无法创建这样的元素,因为React无法跟踪它们。 Instead using "componentDidMount" use regular render. 而是使用“ componentDidMount”使用常规渲染。

render() {
    return <div> okokokkokoko</div>;
}

I highly recommend to go back to React docs and understand it. 我强烈建议您回到React文档并了解它。 https://facebook.github.io/react/tutorial/tutorial.html https://facebook.github.io/react/tutorial/tutorial.html

This issue is occurring because of the code in componentDidMount code 发生此问题是由于componentDidMount代码中的代码

componentDidMount() {
        var elem = document.createElement("div");
        elem.innerText = "okokokkokoko";
        document.getElementById('app').appendChild(elem);
    }

Here after you load /About page, you are appending a div to the body with text "okokokkokoko" . 在这里,在加载/About页面之后,您将在div上附加文本"okokokkokoko" But after you got to /App page, you are not removing this from the page. 但是,进入/App页面后,您将不会从页面中删除它。 Thats why it remains there. 那就是为什么它仍然在那里。

Actually it is bad practice and this is not how react should be used. 实际上,这是一个坏习惯,这不是应该使用反应的方式。

Instead change your /About code's render to following 而是将/About代码的渲染更改为以下内容

render() {
        return (
           <div>
             <div > About < /div>
             <div>okokokokokoko</div>
           </div>
        )
    }

Updated: In that case the following solution might help you 更新:在这种情况下,以下解决方案可能会为您提供帮助

componentDidMount() {
        document.getElementById('abc').innerText = "okokokkokoko";
    }
render() {
        return (
           <div>
             <div > About < /div>
             <div id="abc"></div>
           </div>
        )
    }

Here you are creating a div in the current component. 在这里,您将在当前组件中创建一个div。 So when you will redirect to another route then the entire component (ie including your <div>okokok</div> ) will also unmount. 因此,当您重定向到另一条路线时,整个组件(包括您的<div>okokok</div> )也将卸载。

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

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