对于想了解在反应路由器中检测到先前的路径?的读者,本文将是一篇不可错过的文章,我们将详细介绍在反应路由器中检测到先前的路径怎么办,并且为您提供关于javascript–反应路由器–路由到子组件的路径、
对于想了解在反应路由器中检测到先前的路径?的读者,本文将是一篇不可错过的文章,我们将详细介绍在反应路由器中检测到先前的路径怎么办,并且为您提供关于javascript – 反应路由器 – 路由到子组件的路径、react-router – 反应路由器中的多语言支持、react-router – 如何在反应路由器v4中传递道具?、react-router – 错误刷新路由器反应路由器的有价值信息。
本文目录一览:- 在反应路由器中检测到先前的路径?(在反应路由器中检测到先前的路径怎么办)
- javascript – 反应路由器 – 路由到子组件的路径
- react-router – 反应路由器中的多语言支持
- react-router – 如何在反应路由器v4中传递道具?
- react-router – 错误刷新路由器反应路由器
在反应路由器中检测到先前的路径?(在反应路由器中检测到先前的路径怎么办)
我正在使用React
Router。我想检测我来自何处的上一页(在同一应用程序内)。我在上下文中拥有路由器。但是,我在路由器对象上看不到“上一条路径”或历史记录之类的任何属性。我该怎么做?
答案1
小编典典您可以在componentWillReceiveProps
生命周期方法中保存先前的路径。该逻辑非常接近文档的疑难解答部分中提供的示例react-router
。
<Route component={App}> {/* ... other routes */}</Route>const App = React.createClass({ getInitialState() { return { prevPath: '''' } }, componentWillReceiveProps(nextProps) { if (nextProps.location !== this.props.location) { this.setState({ prevPath: this.props.location }) } }})
最近,可以从州访问它。
javascript – 反应路由器 – 路由到子组件的路径
var sampleApp= React.createClass({ render: function (){ return ( <div className="our-schools-app"> <Header /> <RouteHandler /> <Footer /> </div> ); },}); var routes = ( <Route name="app" path="/" handler={OurSchoolsApp}> <DefaultRoute name="home" handler={HomePage} /> <Route name="add-school" handler={AddSchoolPage} /> <Route name="calendar" handler={CalendarPage} /> <Route name="calendar-detail" path="calendar-detail/:id" handler={CalendarDetailPage} /> <Route name="info-detail" path="info-detail/:id" handler={InfoDetailPage} /> <Route name="info" handler={InfoPage} /> <Route name="news" handler={NewsListPage} /> <Route name="news-detail" path="news-detail/:id" handler={NewsDetailPage} /> <Route name="contacts" handler={ContactPage} /> <Route name="contact-detail" handler={ContactDetailPage} /> <Route name="settings" handler={SettingsPage} /> </Route> ); Router.run(routes,function(Handler){ var mountNode = document.getElementById('app'); React.render(<Handler />,mountNode); });
我想在路线中传递我的页面的标题作为支柱,如下所示:
<Route name="info" handler={InfoPage} pageTitle="Info Page" />
对我的标题组件:
var Header = React.createClass({ render: function () { return ( <div className="bar bar-header" style={this.getStyle()}> <HomeButton /> <div className="h1 title">{this.props.pageTitle}</div> </div> ) } });
但道具显示为空,有人可以帮忙吗?
解决方法
var BaseComponent = React.createClass({ render: function() { return <p>{this.props.text}</p> } }); var FancyComponent = React.createClass({ return <BaseComponent text="fancy!" /> }); var EvenFancierComponent = React.createClass({ return <BaseComponent text="SUPER fancy!" /> });
那么这些路线:
<Route name="standard" handler={BaseComponent} /> <Route name="fancy" handler={FancyComponent} /> <Route name="superfancy" handler={EvenFancierComponent} />
不过关于React Router github issues这个话题的讨论提供了一个好消息:
I just wanted to note here that in the new API (see 07001) routes are
just plain objects,so you can put whatever props you want on them.
Your transition hooks will get a route arg and components will receive
this.props.route,so you should be good to go.
它看起来像是新的1.0版本是beta版,所以应该很快来了!
react-router – 反应路由器中的多语言支持
http://example.com/<somepage> (Russian,default) http://example.com/en/<somepage> (English) http://example.com/jp/<somepage> (Japanese) http://example.com/../ (etc)
当我为所有语言使用前缀时,一切都很好:
<Route path="/:lang"> <Route path="somepage" component={Somepage}/> </Route>
但是对于默认语言,我不需要在url中包含语言,如示例所示.
在可焊接路由器中,可以通过在路径中使用regexp来解决:
path: '/:lang([a-z]{2})?/<somepage>'
但它在反应路由器中不起作用,因为路径必须是字符串,而不是正则表达式.
任何想法如何处理这个用例?
var innerRoutes = ( <Route> <Route path="somepage" component={Somepage}/> <Route path="otherpage" component={Otherpage}/> </Route> ); var routes = ( <Route path="/" component={App}> {innerRoutes} <Route path=":lang"> {innerRoutes} </Route> </Route> );
react-router – 如何在反应路由器v4中传递道具?
基本上,我有一些动态生成的链接.我已通过以下方式将这些链接与Link组件相关联.单击其中一个链接后,用户将路由到新组件.我想将一些数据传递给这些新组件,以便我可以在那里显示它们.
<Link to="dynamic link here">some text</Link>
是否有某种< Link to =“path”data =“data here”> some text< / Link>反应路由器v4的方法?我似乎无法在文档页面中找到它.
<Link to={{ pathname: '/courses',state: { fromDashboard: true } }}> Courses </Link>
然后,您可以从this.props.location.state中获取新路由中的状态
react-router – 错误刷新路由器反应路由器
import React from 'react'; class ProductNew extends React.Component { constructor(props) { super(props); } render(){ return ( <div> <div><h1>ProductNew</h1></div> </div> ); } } module.exports = ProductNew;
页面产品
import React from 'react'; import { Link } from 'react-router'; class Products extends React.Component { constructor(props) { super(props); } render() { return ( <div> <div><h1>Products</h1></div> <Link to="/products/new">new</Link> </div> ); } } module.exports = Products;
页面应用程序
render(( <Router history={createbrowserHistory()}> <Route path="/" component={Layout}> <IndexRoute component={Home} /> <Route path="/products" component={Products}/> <Route path="/products/new" component={ProductNew}/> </Route> </Router> ),document.getElementById('app'));
因此,当我从主路径加载应用程序页面时,一切运行良好,但当我尝试从您的“/ product / new”链接加载时,我收到了类似您的错误.当我放一条绝对路径时,它工作得很好.
我使用webpack捆绑文件和webpack-dev-server来运行开发环境,当你使用webpack-dev-server热加载器(默认情况下)时没有物理文件,这很容易陷入困境.
编辑:
this guy’s question有2个更新,与你有同样的问题,并有比我给你更好的答案/信息.
我们今天的关于在反应路由器中检测到先前的路径?和在反应路由器中检测到先前的路径怎么办的分享就到这里,谢谢您的阅读,如果想了解更多关于javascript – 反应路由器 – 路由到子组件的路径、react-router – 反应路由器中的多语言支持、react-router – 如何在反应路由器v4中传递道具?、react-router – 错误刷新路由器反应路由器的相关信息,可以在本站进行搜索。
本文标签: