Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redirection with React Router Component

Tags:

reactjs

I'm having a problem with react-router-component. I'm trying to make a redirect library that does a "soft" redirect (equivalent to clicking a link, as opposed to window.location = ...) that I can require from a .jsx component.

Per the docs I should just be able to call router.navigate(path) to redirect.

When, for example, I call this on page A, the url in the address bar changes to page B as desired. However, page A simply reloads, leaving the address bar as page B and the display as page A. Suggestions?

like image 891
Logan Howard Avatar asked Aug 19 '14 02:08

Logan Howard


People also ask

How do I redirect to another component in react JS?

To redirect to another page on button click in React: Use the useNavigate() hook, e.g. const navigate = useNavigate(); . Call the navigate() function, passing it the path - navigate('/about') . The navigate() function lets us navigate programmatically.

What are two ways of handling redirect with react router?

Two ways to handle redirecting on a user event such as create, update and delete with React Router.


1 Answers

You should be able to solve this using the Navigation mixin.

Then you can use this.transitionTo with the name of your page.

var PageAdd = React.createClass({

    mixins : [Router.Navigation],

    handleSubmit : function(e) {
        e.preventDefault();

        this.transitionTo('pages');

    },

    render: function () {
        return (
            <form onSubmit={this.handleSubmit}>
                <button type="submit" className="btn btn-default">Submit</button>
            </form>
        );
    }
});

Read more: https://github.com/rackt/react-router/blob/master/docs/api/mixins/Navigation.md

like image 164
Machiel Avatar answered Sep 29 '22 17:09

Machiel