Reactjs 如何使用history.push-in反应
大家好,我创建了一个新操作来登录用户,如下所示:Reactjs 如何使用history.push-in反应,reactjs,react-router,react-redux,Reactjs,React Router,React Redux,大家好,我创建了一个新操作来登录用户,如下所示: import * as types from './actionTypes'; import sessionApi from '../api/SessionApi'; import auth from '../auth/authenticator'; export function loginSuccess() { return {type: types.LOG_IN_SUCCESS} } export function loginUs
import * as types from './actionTypes';
import sessionApi from '../api/SessionApi';
import auth from '../auth/authenticator';
export function loginSuccess() {
return {type: types.LOG_IN_SUCCESS}
}
export function loginUser(credentials) {
return function(dispatch) {
return sessionApi.login(credentials).then(response => {
sessionStorage.setItem('token', response.token);
dispatch(loginSuccess());
}).catch(error => {
throw(error);
});
};
}
export function logOutUser() {
auth.logOut();
return {type: types.LOG_OUT}
}
import * as types from '../actions/actionTypes';
import initialState from './initialState';
export default function sessionReducer(state = initialState.session,
action) {
switch(action.type) {
case types.LOG_IN_SUCCESS:
// history.push('/restaurant')
return !!sessionStorage.token
default:
return state;
}
}
const routes = [
{ path: '/', name: 'Home', component: Home },
{ path: '/login', name: 'Login', component: Login },
{ path: '/home', name: 'Landing', component: Landing },
{ path: '/dishes', exact: true, name: 'Detail', component: Dish },
{ path: '/dishes/detail', name: 'DishDetail', component: Detail },
{ path: '/checkout/registration', name: 'Restaurant', component:
Registration },
];
export default routes;
const routes = [
{ path: '/restaurant', exact: true, name: 'Restaurant', component:
RestrauntDashboard },
{ path: '/restaurant/dashboard', name: 'Restaurant Dashboard',
component: Dashboard },
{ path: '/restaurant/profile', name: 'Restaurant Dashboard', component:
Profile },
];
export default routes;
我为我的授权创建了一个会话缩减器,如下所示:
import * as types from './actionTypes';
import sessionApi from '../api/SessionApi';
import auth from '../auth/authenticator';
export function loginSuccess() {
return {type: types.LOG_IN_SUCCESS}
}
export function loginUser(credentials) {
return function(dispatch) {
return sessionApi.login(credentials).then(response => {
sessionStorage.setItem('token', response.token);
dispatch(loginSuccess());
}).catch(error => {
throw(error);
});
};
}
export function logOutUser() {
auth.logOut();
return {type: types.LOG_OUT}
}
import * as types from '../actions/actionTypes';
import initialState from './initialState';
export default function sessionReducer(state = initialState.session,
action) {
switch(action.type) {
case types.LOG_IN_SUCCESS:
// history.push('/restaurant')
return !!sessionStorage.token
default:
return state;
}
}
const routes = [
{ path: '/', name: 'Home', component: Home },
{ path: '/login', name: 'Login', component: Login },
{ path: '/home', name: 'Landing', component: Landing },
{ path: '/dishes', exact: true, name: 'Detail', component: Dish },
{ path: '/dishes/detail', name: 'DishDetail', component: Detail },
{ path: '/checkout/registration', name: 'Restaurant', component:
Registration },
];
export default routes;
const routes = [
{ path: '/restaurant', exact: true, name: 'Restaurant', component:
RestrauntDashboard },
{ path: '/restaurant/dashboard', name: 'Restaurant Dashboard',
component: Dashboard },
{ path: '/restaurant/profile', name: 'Restaurant Dashboard', component:
Profile },
];
export default routes;
登录成功后,我想使用history.push将我的用户重定向到另一个页面,但我不知道怎么做??我试着先导入
import createBrowserHistory from "history/createBrowserHistory"
然后创建一个新的常量历史,如下所示:
export const history = createBrowserHistory({
forceRefresh: true
})
然后
history.push('/restaurant')
但在行动之后,我从家转到了餐厅。。。。
而不是我右边的部分。
我有两个路由文件,其中一个用于我的主要视图,如下所示:
import * as types from './actionTypes';
import sessionApi from '../api/SessionApi';
import auth from '../auth/authenticator';
export function loginSuccess() {
return {type: types.LOG_IN_SUCCESS}
}
export function loginUser(credentials) {
return function(dispatch) {
return sessionApi.login(credentials).then(response => {
sessionStorage.setItem('token', response.token);
dispatch(loginSuccess());
}).catch(error => {
throw(error);
});
};
}
export function logOutUser() {
auth.logOut();
return {type: types.LOG_OUT}
}
import * as types from '../actions/actionTypes';
import initialState from './initialState';
export default function sessionReducer(state = initialState.session,
action) {
switch(action.type) {
case types.LOG_IN_SUCCESS:
// history.push('/restaurant')
return !!sessionStorage.token
default:
return state;
}
}
const routes = [
{ path: '/', name: 'Home', component: Home },
{ path: '/login', name: 'Login', component: Login },
{ path: '/home', name: 'Landing', component: Landing },
{ path: '/dishes', exact: true, name: 'Detail', component: Dish },
{ path: '/dishes/detail', name: 'DishDetail', component: Detail },
{ path: '/checkout/registration', name: 'Restaurant', component:
Registration },
];
export default routes;
const routes = [
{ path: '/restaurant', exact: true, name: 'Restaurant', component:
RestrauntDashboard },
{ path: '/restaurant/dashboard', name: 'Restaurant Dashboard',
component: Dashboard },
{ path: '/restaurant/profile', name: 'Restaurant Dashboard', component:
Profile },
];
export default routes;
我所有的餐厅观点都是这样的:
import * as types from './actionTypes';
import sessionApi from '../api/SessionApi';
import auth from '../auth/authenticator';
export function loginSuccess() {
return {type: types.LOG_IN_SUCCESS}
}
export function loginUser(credentials) {
return function(dispatch) {
return sessionApi.login(credentials).then(response => {
sessionStorage.setItem('token', response.token);
dispatch(loginSuccess());
}).catch(error => {
throw(error);
});
};
}
export function logOutUser() {
auth.logOut();
return {type: types.LOG_OUT}
}
import * as types from '../actions/actionTypes';
import initialState from './initialState';
export default function sessionReducer(state = initialState.session,
action) {
switch(action.type) {
case types.LOG_IN_SUCCESS:
// history.push('/restaurant')
return !!sessionStorage.token
default:
return state;
}
}
const routes = [
{ path: '/', name: 'Home', component: Home },
{ path: '/login', name: 'Login', component: Login },
{ path: '/home', name: 'Landing', component: Landing },
{ path: '/dishes', exact: true, name: 'Detail', component: Dish },
{ path: '/dishes/detail', name: 'DishDetail', component: Detail },
{ path: '/checkout/registration', name: 'Restaurant', component:
Registration },
];
export default routes;
const routes = [
{ path: '/restaurant', exact: true, name: 'Restaurant', component:
RestrauntDashboard },
{ path: '/restaurant/dashboard', name: 'Restaurant Dashboard',
component: Dashboard },
{ path: '/restaurant/profile', name: 'Restaurant Dashboard', component:
Profile },
];
export default routes;
这是我的app.js:
class App extends Component {
render() {
return (
<HashRouter>
<Switch>
<Route path="/restaurant" name="Restaurant" component= .
{RestrauntDashboard} />
<Route path="/" name="Home" component={Home} />
</Switch>
</HashRouter>
);
}
}
export default App;
类应用程序扩展组件{
render(){
返回(
);
}
}
导出默认应用程序;
最后,我想在用户登录后,使用history.push将其重定向到“/restaurant/path”,感谢您的帮助由于您使用的是HashRouter,您可以使用中提到的道具中的
history
或者,您需要使用createHashHistory
而不是createBrowserHistory
创建历史记录,并将其传递到通用路由器组件,如
import { Router } from 'react-router-dom';
export const history = createHashHistory();
class App extends Component {
render() {
return (
<Router history={history}>
<Switch>
<Route path="/restaurant" name="Restaurant" component= .
{RestrauntDashboard} />
<Route path="/" name="Home" component={Home} />
</Switch>
</HashRouter>
);
}
}
从'react Router dom'导入{Router};
export const history=createHashHistory();
类应用程序扩展组件{
render(){
返回(
);
}
}
除了Shubham的答案之外,中间件是处理所有这些重定向的好地方。一般来说,最好让动作和还原剂保持纯净
可以在动作对象中添加第三个字段,如下所示
export function loginSuccess() {
return {
type: 'types.LOG_IN_SUCCESS',
redirectAfter: '/path/you/wish/to/redirect/after/action'};
}
}
重定向中间件.js
import history from '../history';
const redirectMiddleware = () => next => action => {
if (action.redirectBefore) { // Redirects to the route BEFORE dispatching action to reducers
history.push(action.redirectBefore);
}
const result = next(action);
if (action.redirectAfter) { // Redirects to the router AFTER dispatching action to reducers
history.push(action.redirectAfter);
}
return result;
};
export default redirectMiddleware;
在商店的config.js
中添加中间件
applyMiddleware([redirectMiddleware])
因此,在您的操作中,您可以将redirectBefore作为路径传递,以便在操作到达缩减器之前重定向到该路径,或者如果您希望在操作传递到缩减器之前重定向,则可以将redirectAfter传递到该路径
将您的登录组件更改为如下所示
当用户未登录时,isLoggedIn
是未定义的,因此它不会呈现任何内容,但当登录成功时,会呈现一些分配给令牌的值,结果是
,网站将重定向到/restaurant
如果出现关于isLoggedIn未定义的错误,请将其添加到initialState.js
希望这能解决你的问题。干杯 我已经在我的index.js中创建了它,但这不起作用,我在这个url中,当我单击登录按钮时,他们会将我重定向到我不知道为什么要使用createBrowserHistory,请使用createHashHistory。此外,路由器需要更改以使用自定义历史记录