React Router Dom, Başka bir Uygulamaya nasıl yönlendirilir.herhangi bir rotanın herhangi bir alt rotasındayken js rotası [duplicate]

0

Soru

React & react router dom v5'te yeniyim, ingilizcem de kötü. Bana yardım ettiğin için şimdiden teşekkür ederim.

benim sorunum: Uygulamamda 2 Ana Rotam var.js rotası

import { Suspense } from 'react';
import {BrowserRouter as Router, Switch, Route} from 'react-router-dom'

/* Pges */
import AdminContainer from './Pages/Admin/AdminContainer';
import PublicContainer from './Pages/Public/PublicContainer';
import NotFound from './Pages/NotFound'
import AuthContainer from './Pages/Auth/AuthContainer';

/* Protected Route */

/* Helpers */

function App() {
  console.log("APP")
  return (
    <Suspense fallback={(<p>Loading</p>)}>
      <Router>
        <Switch>
          <Route path="/auth" component={AuthContainer} />
          <Route path="/admin" component={AdminContainer} />
          <Route path="/*" component={PublicContainer} />
          <Route path="*" component={NotFound} />
        </Switch>
      </Router>
    </Suspense>
    )
  }
export default App;

authcontainer'ın 2 alt yolu vardır "/imzala" "/kayıt"

import React from "react";
import {
  BrowserRouter as Router,
  Switch,
  Route,
  withRouter
} from "react-router-dom";

// PAGES
import Signin from "../Auth/Signin";
import Signup from "../Auth/Signup";

const AuthContainer = () => {
  console.log("AUTH")
  return (
    <div>
      <Router>
        <Switch>
          <Route exact path="/auth" component={Signin}/>
          <Route exact path="/auth/signin" component={Signin}/>
          <Route exact path="/auth/signup" component={Signup}/>
        </Switch>
      </Router>
    </div>
  );
};

export default withRouter(AuthContainer);

sonra benim publiccontainer 3 alt yolları var "/" "/ürün" "/mycart"

/* Dependencies */
import { Route, Switch, BrowserRouter as Router } from 'react-router-dom'

/* Components */
import Header from "../../Components/Header"
import Products from "./Products"
import Home from "./Home"
import UserProfile from "../User/AccountProfile"

import MyCart from '../Public/MyCart'

const PublicContainer = () => {
    console.log("PUBLIC")
    return (
        <div>
            <Router>
                <Header />
                <Switch>
                    <Route exact path='/' render={(props) => <Home />} />
                    <Route exact path='/products' render={(props) => <Products />} />
                    <Route exact path='/mycart' render={(props) => <MyCart isAuth={false} />} />
                </Switch>
               </Router>
        </div>
    )
}

export default PublicContainer

sepetim bileşeni yalnızca ısAuth doğruysa işleyecek, aksi takdirde "/auth/signin"e yönlendirecektir.

import React from 'react'
import { Redirect } from 'react-router'

const MyCart = ({isAuth}) => {
    if(!isAuth)
        return (<Redirect  from='*' to='/auth/signin'></Redirect>)
    return (
        <div>
            my cart
        </div>
    )
}

export default MyCart

Sorun şu ki, "/auth/signin "e yönlendirmeye çalışıyor ancak hala" / " sayfasında enter image description here

Yeniden yükle düğmesine bastığımda nihayet "/auth/signin"e yönlendirildi.enter image description here

Bu sorunu nasıl çözebilirim, yardımınız için gerçekten minnettarım

Güncelleme

bu, planlanan rotalarıma genel bir bakış enter image description here

Bu arada, mycart ısAuth yanlış olduğunda, üst url'deki bağlantının doğru şekilde auth signin'e işaret etmesine neden olan /auth/signin'e Bağlanmaya çalıştığını düşünüyorum, ancak bundan sonra yalnızca publiccontainer'ın alt rutinlerini kontrol ediyor.uygulamayı kontrol etmek yerine.js rotaları enter image description here

Ancak yeniden yüklediğimde, uygulamadan doğru rotayı aramaya başlar.beklenen rotayı döndüren js rotaları ve oturum açma sayfası

enter image description here

1

En iyi cevabı

0

Yenile/yeniden yükle düğmesine basıldığında yalnızca doğru yolu oluşturma açısından neredeyse benzer bir soru okudum

burada React Router sadece sayfayı yeniledikten sonra çalışır

Sorun, alt rotaları yeni bir yönlendiriciyle sarmamdı, bu yüzden Anahtarı>> ve diğer alt rutinleri her iki authcontainer'a saran Yönlendirici jsx'i kaldırmayı denedim.js & PublicContainer.js

bu güncelleştirilmiş AuthContainer ' dir.js

import React from "react";
import {
  BrowserRouter as Router,
  Switch,
  Route,
  withRouter
} from "react-router-dom";

// PAGES
import Signin from "../Auth/Signin";
import Signup from "../Auth/Signup";

const AuthContainer = () => {
  console.log("AUTH")
  return (
    <div>
        <Switch>
          <Route exact path="/auth/signin" component={Signin}/>
          <Route exact path="/auth/signup" component={Signup}/>
          <Route exact path="/auth" component={Signin}/>
        </Switch>
    </div>
  );
};

export default withRouter(AuthContainer);

Ve bu PublicContainer var.js

/* Dependencies */
import { Route, Switch } from 'react-router-dom'

/* Components */
import Header from "../../Components/Header"
import Products from "./Products"
import Home from "./Home"
import UserProfile from "../User/AccountProfile"

import MyCart from '../Public/MyCart'

/* Protected */

const PublicContainer = ({toAuth}) => {
    console.log("PUBLIC")
    return (
        <div>
                <Header />
                <Switch>
                    <Route exact path='/products' render={(props) => <Products />} />
                    <Route exact path='/profile' render={(props) => <UserProfile />} />
                    <Route exact path='/mycart' render={(props) => <MyCart />} />
                    <Route exact path='/' render={(props) => <Home />} />
                </Switch>
        </div>
    )
}

export default PublicContainer

enter image description here

2021-11-21 16:31:24

Cevaplarımdan biriyle bağlantılı.
Drew Reese

@ DrewReese Çok Teşekkür Ederim! Çok yaşa
TheNewBeeeee

Diğer dillerde

Bu sayfa diğer dillerde

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................