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

Categories

0 votes
5.1k 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 Reply

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...