Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.6k views
in Technique[技术] by (71.8m points)

javascript - react router redirects to home page on refresh

I have the following code:

let routes = 
        <Switch>
            <Route path="/" exact component={Authentication} />
            <Route path="/test" exact render = {props=> <Authentication/>} />
            <Redirect to="/" />
        </Switch>
        if(this.props.isLoggedIn){
            routes = 
                <>
                    <div className='mobile-header' onClick={this.handleNavBarShow}>
                        <p className="mobile-menu-icon">
                            <i className="fa fa-bars"></i>
                        </p>
                    <p>Title</p>
                    </div>
                    <NavBar toShow = {this.state.showMobileNavBar} 
                            onClicked = {(e)=>this.handleShowAndHide(e,true)}
                    />
                    <Switch>
                        <Route path ="/" exact render = {props=> <Authentication/>}/>   
                        <Route path ="/a" exact render = {props=> <Test1/>}/>
                        <Route path = "/b" exact render ={props =><Test2/>}/>
                        <Route path = "/c" exact render ={props =><Test3/>}/>

                        <Redirect to="/" />
                    </Switch>
                </>
        }
        return (
        <Router>
            <div className = "layout" onClick={(event)=>this.handleShowAndHide(event,false)}>
                {routes} 
                {showSideBar}         
            </div>
        </Router>
        )

problem is when this.props.isLoggedIn === true, whenever i try to reload a route "/a" "/b" "/c", or type it in the adress bar directly it redirects to the "/" page. In the Test1 /Test2 /Test 3 components there are links to one another and it works fine. also it works fine with reloading and direct typing when this.props.isLoggedIn !== true. what could be the problem?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神解答

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...