如何在 React 中使用 # urls 来保存组件应用程序状态?
How to have # urls to save component app state in React?
我们如何在 React(单页应用程序)中获取#url?
1) 访问:https://coinhover.io
2) 点击投资组合
3) 刷新
预计
投资组合组件重新呈现
结果
应用程序中断(我知道这是因为我缺少#)
App.js
import React from 'react'
import { connect } from 'react-redux'
import { BrowserRouter as Router } from 'react-router-dom'
import Routes from './components/Routes'
const supportsHistory = "pushState" in window.history
class App extends React.Component {
render() {
return (
<Router forceRefresh={!supportsHistory}>
<Routes />
</Router>
);
}
}
export default connect(null, null)(App)
Routes.js
import React from 'react'
import { browserHistory, BrowserRouter, hashHistory, Route, Switch } from 'react-router-dom'
import Portfolio from './portfolio/Portfolio'
import Home from './home/Home'
import NoMatch from './NoMatch'
const Routes = () => {
return (
<BrowserRouter history={ browserHistory }>
<Switch>
<Route exact={ true } path="/" component={ Home }/>
<Route exact={ true } path="/portfolio" component={ Portfolio }/>
<Route component={ NoMatch } />
</Switch>
</BrowserRouter>
);
}
export default Routes
使用 HashRouter
解决了我的问题(参见 the docs)
import React from 'react'
import { browserHistory, HashRouter, Route, Switch } from 'react-router-dom'
import Portfolio from './portfolio/Portfolio'
import Home from './home/Home'
import NoMatch from './NoMatch'
const Routes = () => {
return (
<HashRouter history={ browserHistory }>
<Switch>
<Route exact={ true } path="/" component={ Home }/>
<Route exact={ true } path="/portfolio" component={ Portfolio }/>
<Route component={ NoMatch } />
</Switch>
</HashRouter>
);
}
export default Routes
我们如何在 React(单页应用程序)中获取#url?
1) 访问:https://coinhover.io
2) 点击投资组合
3) 刷新
预计
投资组合组件重新呈现
结果
应用程序中断(我知道这是因为我缺少#)
App.js
import React from 'react'
import { connect } from 'react-redux'
import { BrowserRouter as Router } from 'react-router-dom'
import Routes from './components/Routes'
const supportsHistory = "pushState" in window.history
class App extends React.Component {
render() {
return (
<Router forceRefresh={!supportsHistory}>
<Routes />
</Router>
);
}
}
export default connect(null, null)(App)
Routes.js
import React from 'react'
import { browserHistory, BrowserRouter, hashHistory, Route, Switch } from 'react-router-dom'
import Portfolio from './portfolio/Portfolio'
import Home from './home/Home'
import NoMatch from './NoMatch'
const Routes = () => {
return (
<BrowserRouter history={ browserHistory }>
<Switch>
<Route exact={ true } path="/" component={ Home }/>
<Route exact={ true } path="/portfolio" component={ Portfolio }/>
<Route component={ NoMatch } />
</Switch>
</BrowserRouter>
);
}
export default Routes
使用 HashRouter
解决了我的问题(参见 the docs)
import React from 'react'
import { browserHistory, HashRouter, Route, Switch } from 'react-router-dom'
import Portfolio from './portfolio/Portfolio'
import Home from './home/Home'
import NoMatch from './NoMatch'
const Routes = () => {
return (
<HashRouter history={ browserHistory }>
<Switch>
<Route exact={ true } path="/" component={ Home }/>
<Route exact={ true } path="/portfolio" component={ Portfolio }/>
<Route component={ NoMatch } />
</Switch>
</HashRouter>
);
}
export default Routes