React router 怎么取回退历史数据

2025-04-14 12:20:11
推荐回答(1个)
回答1:

import React from 'react'
import { render } from 'react-dom'

const About = React.createClass({/*...*/})
const Inbox = React.createClass({/*...*/})
const Home = React.createClass({/*...*/})

const App = React.createClass({
getInitialState() {
 return {
   route: window.location.hash.substr(1)
 }
},

componentDidMount() {
 window.addEventListener('hashchange', () => {
   this.setState({
     route: window.location.hash.substr(1)
   })
 })
},

render() {
 let Child
 switch (this.state.route) {
   case '/about': Child = About; break;
   case '/inbox': Child = Inbox; break;
   default:      Child = Home;
 }

 return (
   
     App
     


  •        
  • About

  •        
  • Inbox

  •      

     
   
 )
}
})

render(, document.body)