Showing posts with label reactrouter. Show all posts
Showing posts with label reactrouter. Show all posts

Friday, January 31, 2020

ReactJS - You tried to redirect to the same route you're currently on

It's a common problem when you redirect on same route will give a warning like You tried to redirect to the same route you're currently on: /profile/2. It's because of when we define Route we can not define for every parameters like 1, 2, 3 etc. Normally we define our Route like:

<Route path="/profile/:id" component={Profile} />

And this will handle every URL like http://example.com/profile/1, http://example.com/profile/2 etc.
But sometimes when we use Redirect component to redirect to another profile page from current profile page using the below code

return <Redirect to={"/profile/" + this.state.id} push={true} />;

The above warning thrown because our Route is same for all URL like /profile/1 as well as /profile/2.
To avoid this warning we need to define key to our Route like below

<Route path="/profile/:id" render={props => { return <Profile {...props} key={props.match.params.id} />; }} />
Full example is created on CodeSandbox.IO https://codesandbox.io/s/you-tried-to-redirect-to-the-same-route-youre-currently-on-063ot

Saturday, December 28, 2019

Implementing Programically Redirect With React Router | React redirect to component | Redirect with React Router

A lot of these short blog posts are just for me to find later when I forget how to do something.
So, there is an span where there is a click event named goToAnotherPage, when clicked I just set state the edit variable true, and it redirect to another page using react component named Redirect as described in function goToAnotherPage.
import React, { Component } from 'react'
import {Redirect} from 'react-router-dom'

class SomePage extends Component {
    constructor () {
        super();
        this.state = {
            edit: false
        };
        this.goToAnotherPage = this.goToAnotherPage.bind(this);
    }

    componentDidMount () {
        super.componentDidMount();
    }

    goToAnotherPage(e) {
        this.setState({'edit': true});
    }

    render () {
        if (this.state.edit) {
            return <Redirect to={'/go-to-another-page'} push={true}/>
        }
        return (
            <span onClick={(e) => this.goToAnotherPage(e)}>Go to another page</span>
        )
    }
}

export default SomePage

Thursday, December 19, 2019

ReactJS - Multiple Layout In React Using React Router | Using multiple layouts for react-router components

React Router is a standard routing library for React.
In the first step, we will create two different layout files (named CommonLayout and DashboardLayout) and their respective routes to implement multiple layouts in React.

CommonLayout


import React, { Component } from 'react';
import {Route} from 'react-router-dom';

class CommonLayoutComponent extends React.Component{
    constructor(props){
        super(props);
        this.createRef = this.createRef.bind(this);
    }

    createRef(element) {
        console.log(element);
    }

    render() {
        return (
            <div key="dashboard">
                <div className="Content" ref={this.createRef}>
                    {this.props.children}
                </div>
            </div>
        );
    }
}

const CommonLayoutRoute = ({component: Component, ...rest}) => {
    return (
        <Route {...rest} render={matchProps => (
            <CommonLayoutComponent {...matchProps}>
                <Component {...matchProps} />
            </CommonLayoutComponent>
        )} />
    )
};

export default CommonLayoutRoute;

DashboardLayout


import React, { Component } from 'react';
import { Route } from 'react-router-dom';

const DashboardLayoutRoute = ({component: Component, ...rest}) => {
    return (
        <Route {...rest} render={matchProps => (
            <div>
                <h3>Dashboard Layout</h3>
                <Component {...matchProps} />
            </div>
        )} />
    )
};

export default DashboardLayoutRoute;
Now, update your App.js file to use both - the layout and the component - as below.
import React, { Component } from 'react';
import { BrowserRouter as Router, Route, Redirect, Switch } from 'react-router-dom';

/** Layouts **/
import CommonLayoutRoute from "./CommonLayoutRoute";
import DashboardLayoutRoute from "./DashboardLayout";

/** Components **/
import LayoutPage1 from './LayoutPage1';
import LayoutPage2 from './LayoutPage2'

/*
   App
 */
class App extends Component {
    render() {
        return (
            <Router>
                <Switch>
                    <Route exact path="/">
                        <Redirect to="/layout1" />
                    </Route>
                    <CommonLayoutRoute path="/layout1" component={LayoutPage1} />
                    <DashboardRoute path="/layout2" component={LayoutPage2} />
                </Switch>
            </Router>
        );
    }
}

export default App;
Here, we have updated the App.js file as defined in the route. When we route to /layout1, then CommonLayoutRoute (our first layout) will be used as the master page for the component LayoutPage1. On the other hand, when we route to /layout2, then our second layout, i.e., DashbaordLayoutRoute will be used as the master page for the component LayoutPage2.