如何在React路由器中的组件内重定向?我正在使用react-rouer import React from 'react'import _ from 'lodash';import FavoriteItem from '../components/FavoriteItem'const FavoritesList = ({list}) = { let transitionToItem = ({id
import React from 'react' import _ from 'lodash'; import FavoriteItem from '../components/FavoriteItem' const FavoritesList = ({list}) => { let transitionToItem = ({id}) => { // make transition to `/details/${id}` }; return ( <ul className="list-group favorites-list"> <FavoriteItem onFavoriteClick={ () => { transitionToItem(item); } } key={item.id} item={item}/> </ul> ); }; export default FavoritesList
路线:
<Route path="details/:item" name="details" component={DetailsPage}/>我认为这样做的首选方法是在react-router Link元素中包含你点击的任何元素,然后让路由器处理所有内容.
<Link to=`/details/${item_id}`>Click me!</Link>
如果你肯定想采取你的方法,将browserHistory模块从react-router导入你的组件并将新URL直接推到它上面.
import { browserHistory } from 'react-router'; ... let transitionToItem = ({id}) => { browserHistory.push(`/details/${id}`); };