IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

React Discussion :

React router redirection


Sujet :

React

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre éprouvé
    Profil pro
    Inscrit en
    Juin 2013
    Messages
    1 225
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juin 2013
    Messages : 1 225
    Par défaut React router redirection
    J'utilise le template Dashboard de material ui et j'ai ajouté une page de connexion. Quand je rentre http://localhost:3000, je suis bien redirigé vers http://localhost:3000/login

    Mais quand je clique dans mon menu pour atteindre la page profil (http://localhost:3000/use), je suis de nouveau redirigé vers la page de connexion (http://localhost:3000/login).

    Je ne comprends pas le problème.

    Mon fichier(dossier) principal de parcours(routes) :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    import Login from "views/authentification/LoginPage.jsx";
    import Dashboard from "layouts/Dashboard/Dashboard.jsx";
     
    const indexRoutes = [
    { 
        path: "/login", 
        component: Login
    },
    { 
        path: "/dashboard", 
        component: Dashboard 
    },
    { 
        redirect: true, 
        path: "/", 
        to: "/login"
    }
    ];
     
    export default indexRoutes;
    Mon fichier de route Dahboard :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    // @material-ui/icons
    import Dashboard from "@material-ui/icons/Dashboard";
    import Person from "@material-ui/icons/Person";
    // import ContentPaste from "@material-ui/icons/ContentPaste";
    import LibraryBooks from "@material-ui/icons/LibraryBooks";
    import BubbleChart from "@material-ui/icons/BubbleChart";
    import LocationOn from "@material-ui/icons/LocationOn";
    import Notifications from "@material-ui/icons/Notifications";
    import Unarchive from "@material-ui/icons/Unarchive";
    // core components/views
    import DashboardPage from "views/Dashboard/Dashboard.jsx";
    import UserProfile from "views/UserProfile/UserProfile.jsx";
    import TableList from "views/TableList/TableList.jsx";
    import Typography from "views/Typography/Typography.jsx";
    import Icons from "views/Icons/Icons.jsx";
    import Maps from "views/Maps/Maps.jsx";
    import NotificationsPage from "views/Notifications/Notifications.jsx";
    import UpgradeToPro from "views/UpgradeToPro/UpgradeToPro.jsx";
     
    const dashboardRoutes = [
    {
    path: "/dashboard",
    sidebarName: "Dashboard",
    navbarName: "Material Dashboard",
    icon: Dashboard,
    component: DashboardPage
    },
    { 
    path: "/user",
    exact: true,
    sidebarName: "User Profile",
    navbarName: "Profile",
    icon: Person,
    component: UserProfile
    },
    {
    path: "/table",
    sidebarName: "Table List",
    navbarName: "Table List",
    icon: "content_paste",
    component: TableList
    },
    ..........
    ];
     
    export default dashboardRoutes;

  2. #2
    Membre Expert
    Avatar de gwyohm
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Octobre 2007
    Messages
    925
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 45
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Octobre 2007
    Messages : 925
    Par défaut
    tu as oublié le r dans ton code ou en écrivant ce message (http://localhost:3000/use vs http://localhost:3000/user)
    Si tu n'as pas mis l'option exact sur ton mapping de "/" vers "login", "/use" va faire un match sur "/" et donc te renvoyer sur le login

  3. #3
    Membre éprouvé
    Profil pro
    Inscrit en
    Juin 2013
    Messages
    1 225
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juin 2013
    Messages : 1 225
    Par défaut
    Même en mettant l'option exact sa marche pas:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
        { 
            redirect: true, 
            exact: true,
            path: "/", 
            to: "/login"
        }

  4. #4
    Membre Expert
    Avatar de gwyohm
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Octobre 2007
    Messages
    925
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 45
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Octobre 2007
    Messages : 925
    Par défaut
    OK, mais est-ce que le résultat est différent ?
    A propos du r (use / user), t'es bon dans test tests, c'était une erreur dans ton premier post ?
    Les autres routes fonctionnent ? (table)
    je n'ai pas de vue d'ensemble, donc j'essaye de deviner...
    C'est pas /dashboard/user ta route plutôt ?

  5. #5
    Membre éprouvé
    Profil pro
    Inscrit en
    Juin 2013
    Messages
    1 225
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juin 2013
    Messages : 1 225
    Par défaut
    Pardons, voici le fichier complet mis à jour des routes du dashboard :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    // @material-ui/icons
    import Dashboard from "@material-ui/icons/Dashboard";
    import Person from "@material-ui/icons/Person";
    // import ContentPaste from "@material-ui/icons/ContentPaste";
    import LibraryBooks from "@material-ui/icons/LibraryBooks";
    import BubbleChart from "@material-ui/icons/BubbleChart";
    import LocationOn from "@material-ui/icons/LocationOn";
    import Notifications from "@material-ui/icons/Notifications";
    import Unarchive from "@material-ui/icons/Unarchive";
    // core components/views
    import DashboardPage from "views/Dashboard/Dashboard.jsx";
    import UserProfile from "views/UserProfile/UserProfile.jsx";
    import TableList from "views/TableList/TableList.jsx";
    import Typography from "views/Typography/Typography.jsx";
    import Icons from "views/Icons/Icons.jsx";
    import Maps from "views/Maps/Maps.jsx";
    import NotificationsPage from "views/Notifications/Notifications.jsx";
    import UpgradeToPro from "views/UpgradeToPro/UpgradeToPro.jsx";
     
    const dashboardRoutes = [
      {
        path: "/dashboard",
        sidebarName: "Dashboard",
        navbarName: "Material Dashboard",
        icon: Dashboard,
        component: DashboardPage
      },
      {
        path: "/user",
        sidebarName: "User Profile",
        navbarName: "Profile",
        icon: Person,
        component: UserProfile
      },
      {
        path: "/table",
        sidebarName: "Table List",
        navbarName: "Table List",
        icon: "content_paste",
        component: TableList
      },
      {
        path: "/typography",
        sidebarName: "Typography",
        navbarName: "Typography",
        icon: LibraryBooks,
        component: Typography
      },
      {
        path: "/icons",
        sidebarName: "Icons",
        navbarName: "Icons",
        icon: BubbleChart,
        component: Icons
      },
      {
        path: "/maps",
        sidebarName: "Maps",
        navbarName: "Map",
        icon: LocationOn,
        component: Maps
      },
      {
        path: "/notifications",
        sidebarName: "Notifications",
        navbarName: "Notifications",
        icon: Notifications,
        component: NotificationsPage
      },
      {
        path: "/upgrade-to-pro",
        sidebarName: "Upgrade To PRO",
        navbarName: "Upgrade To PRO",
        icon: Unarchive,
        component: UpgradeToPro
      }
      //{ redirect: true, path: "/", to: "/dashboard", navbarName: "Redirect" } : Poser problème pour la redirection après la connexion
    ];
     
    export default dashboardRoutes;
    Aucunes ne fonctionne. Elle redirige vers la page de login.


    Voici le layouts du Dashboard :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    /* eslint-disable */
    import React from "react";
    import PropTypes from "prop-types";
    import { Switch, Route, Redirect } from "react-router-dom";
    // creates a beautiful scrollbar
    import PerfectScrollbar from "perfect-scrollbar";
    import "perfect-scrollbar/css/perfect-scrollbar.css";
    // @material-ui/core components
    import withStyles from "@material-ui/core/styles/withStyles";
    // core components
    import Header from "components/Header/Header.jsx";
    import Footer from "components/Footer/Footer.jsx";
    import Sidebar from "components/Sidebar/Sidebar.jsx";
     
    import dashboardRoutes from "routes/dashboard.jsx";
     
    import dashboardStyle from "assets/jss/material-dashboard-react/layouts/dashboardStyle.jsx";
     
    import image from "assets/img/sidebar-2.jpg";
    import logo from "assets/img/reactlogo.png";
     
    const switchRoutes = (
      <Switch>
        {dashboardRoutes.map((prop, key) => {
          if (prop.redirect)
            return <Redirect from={prop.path} to={prop.to} key={key} />;
          return <Route path={prop.path} component={prop.component} key={key} />;
        })}
      </Switch>
    );
     
    class App extends React.Component {
      constructor(props) {
        super(props);
        this.state = {
          mobileOpen: false
        };
        this.resizeFunction = this.resizeFunction.bind(this);
      }
      handleDrawerToggle = () => {
        this.setState({ mobileOpen: !this.state.mobileOpen });
      };
      getRoute() {
        return this.props.location.pathname !== "/maps";
      }
      resizeFunction() {
        if (window.innerWidth >= 960) {
          this.setState({ mobileOpen: false });
        }
      }
      componentDidMount() {
        if (navigator.platform.indexOf("Win") > -1) {
          const ps = new PerfectScrollbar(this.refs.mainPanel);
        }
        window.addEventListener("resize", this.resizeFunction);
      }
      componentDidUpdate(e) {
        if (e.history.location.pathname !== e.location.pathname) {
          this.refs.mainPanel.scrollTop = 0;
          if (this.state.mobileOpen) {
            this.setState({ mobileOpen: false });
          }
        }
      }
      componentWillUnmount() {
        window.removeEventListener("resize", this.resizeFunction);
      }
      render() {
        const { classes, ...rest } = this.props;
        return (
          <div className={classes.wrapper}>
            <Sidebar
              routes={dashboardRoutes}
              logoText={"Creative Tim"}
              logo={logo}
              image={image}
              handleDrawerToggle={this.handleDrawerToggle}
              open={this.state.mobileOpen}
              color="blue"
              {...rest}
            />
            <div className={classes.mainPanel} ref="mainPanel">
              <Header
                routes={dashboardRoutes}
                handleDrawerToggle={this.handleDrawerToggle}
                {...rest}
              />
              {/* On the /maps route we want the map to be on full screen - this is not possible if the content and conatiner classes are present because they have some paddings which would make the map smaller */}
              {this.getRoute() ? (
                <div className={classes.content}>
                  <div className={classes.container}>{switchRoutes}</div>
                </div>
              ) : (
                <div className={classes.map}>{switchRoutes}</div>
              )}
              {this.getRoute() ? <Footer /> : null}
            </div>
          </div>
        );
      }
    }
     
    App.propTypes = {
      classes: PropTypes.object.isRequired
    };
     
    export default withStyles(dashboardStyle)(App);
    quand je tape http://localhost:3000/dashboard/user, sa reste sur la page du Dashboard

  6. #6
    Membre éprouvé
    Profil pro
    Inscrit en
    Juin 2013
    Messages
    1 225
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juin 2013
    Messages : 1 225
    Par défaut
    Voici aussi comment je redirige après la connexion:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
          return <Redirect to={"/dashboard"}/>;

Discussions similaires

  1. React native : provider react-native-router-flux
    Par sbill dans le forum Général JavaScript
    Réponses: 0
    Dernier message: 10/03/2017, 13h36
  2. Réponses: 3
    Dernier message: 02/04/2015, 14h05
  3. [.htaccess] urlrewriting + router php = redirection
    Par Hive.42 dans le forum Apache
    Réponses: 4
    Dernier message: 01/07/2013, 17h34
  4. [reseaux] redirection de flux
    Par Olive1808 dans le forum Programmation et administration système
    Réponses: 2
    Dernier message: 12/08/2002, 09h24

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo