6

How to make redirect inside component in React router? I'm using react-rouer

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:

<Route path="details/:item" name="details" component={DetailsPage}/>

1 Answer 1

6

I think the preferred way to do this would be to wrap whatever element you're clicking on in a react-router Link element and let the router handle everything.

<Link to=`/details/${item_id}`>Click me!</Link>

If you definitely want to take your approach, import the browserHistory module from react-router into your component and push the new URL right onto it.

import { browserHistory } from 'react-router';
...
let transitionToItem = ({id}) => {
  browserHistory.push(`/details/${id}`);
};

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.