我正在试图找出如何使用React VR插入React路由器.
首先,我应该使用react-router dom / native吗?目前尚不清楚,因为React VR构建在React Native之上,但在浏览器中运行.
这是我遇到问题的代码.
import React from 'react'; import { AppRegistry } from 'react-vr'; import { BrowserRouter as Router,Route } from 'react-router-dom' import Landing from './components/Landing'; import Video from './components/Video'; export default class WelcomeToVR extends React.Component { render() { return ( <Router> <Route exact path={'/vr/'} component={Landing} /> <Route exact path={'/vr/video/'} component={Video} /> </Router> ); } }; AppRegistry.registerComponent('WelcomeToVR',() => WelcomeToVR);
我使用react-router 4.1.2来提供基于
remydib提到的Ryan Florence视频的解决方案.
在主应用程序组件中:
import { MemoryRouter,Redirect,Route,Switch } from 'react-router'; ... <MemoryRouter> ... </MemoryRouter>
在使用VrButton的组件中:
export class NavBtn extends React.Component { static contextTypes = { router: React.PropTypes.object.isrequired,}; render() { const { to } = this.props; const onClick = () => this.context.router.history.push(to); return ( <VrButton onClick={onClick}> ... </VrButton> ); } }
在npm中有react-router-vr包,但它看起来只是占位符.遗憾的是,此刻不支持浏览器URL.