Javascript 在react js中重定向到主页
我有一个问题,当我刷新页面时,它会返回到主页,因为当我检查用户是否使用Firebase登录时,它会自动重定向到主页。 有人可以帮我解决这个问题,提前谢谢你。 App.js:Javascript 在react js中重定向到主页,javascript,reactjs,react-redux,Javascript,Reactjs,React Redux,我有一个问题,当我刷新页面时,它会返回到主页,因为当我检查用户是否使用Firebase登录时,它会自动重定向到主页。 有人可以帮我解决这个问题,提前谢谢你。 App.js: 从“React”导入React; 从“./Component/Navbarr/Navbarr”导入Navbarr; 从“./Component/Home/Home”导入Home; 从“./Component/About/About”导入关于; 从“./Component/Contact/Contact”导入联系人; 从“.
从“React”导入React;
从“./Component/Navbarr/Navbarr”导入Navbarr;
从“./Component/Home/Home”导入Home;
从“./Component/About/About”导入关于;
从“./Component/Contact/Contact”导入联系人;
从“./Component/Javascript/Javascript”导入Javascript;
从“./Component/Php/Php”导入Php;
从“./Component/Profile/Profile”导入配置文件;
从“./Component/Python/Python”导入Python;
从“./Component/Askquestion/Askquestion”导入Askquestion;
从“./组件/物品/物品”导入物品;
从“./Component/Login/Login”导入登录名;
从“./Component/Register/Register”导入寄存器;
导入“/App.css”;
从'react redux'导入{connect};
从“react Router dom”导入{BrowserRouter as Router,Switch,Route};
从“./Component/SidePanel/SidePanel”导入侧面板;
类应用程序扩展了React.Component{
建造师(道具){
超级(道具);
这个州={
}
}
render()
{
返回(
);
}
}
常量mapstatetoprops=状态=>({
currentUser:state.user.currentUser
})
导出默认连接(MapStateTops)(应用程序);
Index.js
从“React”导入React;
从“react dom”导入react dom;
导入“./index.css”;
从“./App”导入应用程序;
从'react redux'导入{Provider,connect};
从“redux”导入{createStore};
将*作为serviceWorker从“/serviceWorker”导入;
从“react Router dom”导入{BrowserRouter as Router,Switch,Route,withRouter};
从'redux devtools extension'导入{composeWithDevTools};
从“./Reducers/Reducers”导入rootReducer;
从“./Component/Login/Login”导入登录名;
从“./Component/Register/Register”导入寄存器;
从“/Actions/Actions”导入{setuser,clearUser};
从“./Component/firebase/firebase”导入firebase;
从“./Component/Spinner/Spinner”导入微调器;
const store=createStore(rootReducer,composeWithDevTools());
类根扩展了React.Component{
componentDidMount(){
firebase.auth().onAuthStateChanged(用户=>{
如果(用户){
const currentroute=this.props.history.location.pathname;
this.props.setuser(用户);
this.props.history.push('/');
}否则{
this.props.history.push(“/Login”)
this.props.clearUser();
}
})
}
render(){
是否返回此.props.isLoading?:(
)
}
}
const mapStateFromProps=state=>({
isLoading:state.user.isLoading
})
const RootWithAuth=withRouter(connect(mapStateFromProps,{setuser,clearUser})(Root));
ReactDOM.render(
,document.getElementById('root'))
);
serviceWorker.unregister();
大家只想知道,如果你愿意,我可以在GitHub上提供这个项目 检查用户是否登录后,可以重定向到currentpath,而不是重定向到主页
firebase.auth().onAuthStateChanged(user => {
if (user) {
const currentroute = this.props.location.pathname;
this.props.setuser(user);
this.props.history.push(currentroute);
} else {
this.props.history.push('/Login')
this.props.clearUser();
}
})
你好,哈里克里希南,谢谢你的回答,当我这样做时,它保持在同一路线上,但它在页面上没有显示任何内容,它是空的。您可以将该项目添加到类似的位置吗codesanbox@Salah请删除
根目录中的路由
组件的确切路径。非常感谢。现在我发布了错误消息。当它将您重定向回哪个页面时,您在哪个页面上?嗨@RedBaron,不管是什么页面,假设我在名为contact的页面中,一旦我刷新它,就会将我重定向到主页。您是否尝试过为每个路由添加
,如下所示:
@RedBaron我从根组件中的路由中删除了exact,并且它可以工作
import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
import {Provider,connect} from 'react-redux';
import {createStore} from 'redux';
import * as serviceWorker from './serviceWorker';
import {BrowserRouter as Router,Switch,Route,withRouter} from 'react-router-dom';
import {composeWithDevTools} from 'redux-devtools-extension';
import rootReducer from './Reducers/Reducers';
import Login from './Component/Login/Login';
import Register from './Component/Register/Register';
import{setuser,clearUser} from './Actions/Actions';
import firebase from './Component/firebase/firebase';
import Spinner from './Component/Spinner/Spinner';
const store=createStore(rootReducer,composeWithDevTools());
class Root extends React.Component{
componentDidMount(){
firebase.auth().onAuthStateChanged(user=>{
if(user){
const currentroute=this.props.history.location.pathname;
this.props.setuser(user);
this.props.history.push('/');
}else{
this.props.history.push('/Login')
this.props.clearUser();
}
})
}
render(){
return this.props.isLoading ? <Spinner/>: (
<Switch>
<Route exact path='/' component={App}/>
<Route path='/Register' component={Register}/>
<Route path='/Login' component={Login}/>
</Switch>
)
}
}
const mapStateFromProps=state=>({
isLoading:state.user.isLoading
})
const RootWithAuth=withRouter(connect(mapStateFromProps,{setuser,clearUser})(Root));
ReactDOM.render(
<Provider store={store}>
<Router>
<RootWithAuth />
</Router>
</Provider>
,document.getElementById('root')
);
serviceWorker.unregister();
firebase.auth().onAuthStateChanged(user => {
if (user) {
const currentroute = this.props.location.pathname;
this.props.setuser(user);
this.props.history.push(currentroute);
} else {
this.props.history.push('/Login')
this.props.clearUser();
}
})