Redux -我已经改变了我的状态,在这之后我该怎么做呢?
Redux - I've changed my state how do I make things happen after this?
我正在用redux编写我的登录系统,我觉得我应该从更简单的东西开始我的第一个redux:D
但:
减速器
function application (state = initialState, action) {
switch (action.type) {
case 'LOGIN_REQUEST':
return Object.assign({}, state, { loggedIn: true, shouldRedirect: true });
case 'LOGGED_IN':
return Object.assign({}, state, { loggedIn: true, shouldRedirect: true });
case 'LOGIN_FAILED':
return Object.assign({}, state, { loggedIn: false, shouldRedirect: false, errorMessage: action.error.message })
}
return state
}
<<p> 操作/strong> function receiveLogin(user) {
return {
type: LOGIN_SUCCESS,
isFetching: false,
isAuthenticated: true
}
}
function loginError(message) {
return {
type: LOGIN_FAILURE,
isFetching: false,
isAuthenticated: false,
message
}
}
export function submitLogin (creds) {
if(creds.email.length && creds.pw.length){
return receiveLogin();
}else{
return loginError();
}
}
class SignIn extends React.Component {
logIn = ( e ) => {
console.info('as');
e.preventDefault ();
store.dispatch(authAction.submitLogin(this.state));
};
render = function () {
return (
<app-content class={loggedOut.home}>
<Logo />
<h1>Welcome to <br/> Kindred</h1>
<h2>Please sign in to your account</h2>
<form role="form" onSubmit={this.logIn}>
<input type="text" name="email" onChange={this.changed} component="input" placeholder="Enter email address"/>
<input type="password" name="pw" onChange={this.changed} component="input" placeholder="Password"/>
<button>Sign In</button>
</form>
<div className={loggedOut.extraDetails}>
<Link to="/signup">Don't have an account yet?</Link>
<Link to="/skip" className={loggedOut.extraDetailsRight}>Skip</Link>
</div>
</app-content>
)
}
};
表示LOGGED_IN
状态改变。我究竟如何将我的状态与转发到另一个页面联系起来?我不能在我的减速器中加入任何这样的逻辑,这些必须是纯的。但是我需要将用户转发到另一个页面。我已经有了react-route,它上面有HOC来监视那些有或没有登录状态的人。但这只在渲染:/我不能把它添加到这个逻辑,因为这会触发每一个单独的页面查看
如果你的应用需要重定向,你可以重定向,而不是在成功时触发操作。重定向不是React组件或redux操作。如果有的话,它可以是异步操作创建器的一部分。
您可以通过订阅actions来监听您的store。商店。当您的商店分派操作时,subscribe(handlerFn)应该调用处理程序函数。在处理程序中,您可以检查状态并相应地做出反应,请原谅我的双关语。
您可以为路由创建redux中间件。因此,一旦您登录了,您就可以调度一个动作,让中间件拦截它,然后它开始行动并路由到下一个页面。因为你使用的是react-router,中间件可以是这样的:
/** Middleware */
import { browserHistory } from 'react-router';
let navigate = ( path) => {
browserHistory.push(path);
};
export default store => next => action => {
next(action);
switch ( action.type ){
case ActionTypes.NAV_TO_POST_LOGIN :
navigate('/postLogin');
break;
}
}
相关文章:
- 硒元素在这一点上是不可点击的
- 在XMLHttpRequest之后重新初始化jQuery
- 在jAlert之后设置焦点
- Promise函数在.then之后未运行函数化
- 问号在这段代码中是什么意思
- React Router,在params之后嵌套路由
- 在.append之后运行函数
- 使文本在它之后再次可见'It’s被一个按钮隐藏了
- 应用程序在“关闭”之后停止工作(控制台中没有错误);咕哝的构造”;
- 我怎样才能(仅使用 ajax)在 setInterval 之后保持所选值处于选中状态
- 为什么我的状态在 React.js 中没有更新
- 每次在动画之后完全删除该元素.- 仅使用 CSS
- jQuery Slider/Carousel 在 DOM 之后更新内容
- 在指令之后加载的角度ng-init函数
- CKEditor - 插入文本在 setData 之后不起作用
- 流星技术/模式,用于等待数据库变量更改,然后在 in 之后执行某些操作
- Angular指令mouseenter/mouseleave正在工作,但在mouseleave之后不会设置为初始状态
- 在setState之后落后的一个状态
- React:如何强制状态更改在 setState 之后发生
- Redux -我已经改变了我的状态,在这之后我该怎么做呢?