我有一些在ReactRouter节点/组件外部创建的动态创建的组件,因此ReactRouter认为它们不在上下文中,如果我尝试使用ReactRouter.Link
,则会抛出错误:
Links rendered outside of a router context cannot handle clicks
我找到了一个解决方法:
var routes = ReactDOM.render((
<ReactRouter.Router history={ReactRouter.browserHistory}>
<ReactRouter.Route path="/" component={Main}>
<ReactRouter.IndexRoute component={Homepage}/>
<ReactRouter.Route path="about" component={About} />
</ReactRouter.Route>
</ReactRouter.Router>
), document.getElementById('routes'));
//So items outside routes can still use the router
routes.goTo = function(route)
{
//Go to the route
routes.history.push( route );
};
这有效,但是还有一种获取/设置上下文并使用它来导航的方法吗?