V
V
Vladislav2018-04-01 22:10:13
JavaScript
Vladislav, 2018-04-01 22:10:13

How to pass props when redirecting via React-Router?

There is a component with a push notification,
5ac12d891e375631728000.png
it should occur if the user wants to take a walk along private routes and will be returned to the main page via Redirect How to implement this? I so understood that it is impossible to transfer anything through Redirect.
Here, if you need a notification component (PopupNotification.jsx)

PopupNotification
import React, {Component} from 'react';

import $ from 'jquery';

export default class PopupNotification extends Component{
    componentDidMount(){
            var notifyElement = $(".popup").addClass('active');
            $('.popup').prepend(notifyElement);
            setTimeout(function (ele) {
                $(".popup").removeClass('active');
            }, 3000, notifyElement);
    }
    render(){
    return(
        <div className="popup">
            <i className="fa fa-exclamation-circle "></i>
            <span className="popup_title">You must be logged in!</span>
        </div>
    )
}
}


Here is the private route component:
PrivateRoutes.jsx
import React from 'react';

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

export default function PrivateRoute({user, component: Component, data, onLogout, ...rest}){
    return(
        <Route {...rest} render={
            props=> (
                user ?
                    <Component data={data} onLogout={onLogout}{...props} />
                    :
                    <Redirect to="/" />     
            )
        } />
    );
}


Here, just in case, is an example of such a private route:
<PrivateRoute exact path="/videocourses"  onLogout={this.logout}  data={videoCoursesDB}  user={this.state.user}  component={VideoCourses}/>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Nikolaev, 2018-04-01
@Cetch

Change state, on the main page, see what you have in state. Depends on what exactly in props you need on the main page.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question