我有这个Protected
HOC。其目的是在用户通过身份验证时仅呈现其WrappedComponent
。否则应该呈现AuthenticateComponent
(通常是登录组件)。包装组件仍然呈现,当REDX状态说不应该
import React from "react"
const PROPTYPES = {
authenticated: React.PropTypes.bool.isRequired,
}
export default (WrappedComponent, AuthenticateComponent) => {
let Protected = (props) => (
props.authenticated
? <WrappedComponent {...props}/>
: <AuthenticateComponent {...props}/>
)
Protected.propTypes = PROPTYPES
return Protected
}
该组件的道具来自一个连接Redux的容器组件
const AccountContainer = ({ children }) => (
<div>{children}</div>
)
const select = state => state.account
export default connect(select, { refreshUser, logout })(Protected(AccountContainer, LoginContainer))
我account
减速看起来是这样的:
function authenticated(state = false, action) {
switch (action.type) {
case actions.START_SIGNUP_SUCCESS:
case actions.LOGIN_SUCCESS:
return true
case actions.LOGIN_ERROR:
case actions.START_SIGNUP_ERROR:
case actions.LOGOUT_SUCCESS:
return false
default:
return state
}
}
...
export default combineReducers({
authenticated,
access_token,
loggingIn,
user,
error,
})
现在情况发生在LOGOUT
动作设定state.account.authenticated
属性设置为false,但仍然呈现WrappedComponent
。它访问account
的各种其他属性,并且它们都已被清除,组件未检查和期望。 WrappedComponent
假定当它被呈现时,account
状态仍然是authenticated
并且因此是有效的。
我想知道可能是什么样的竞争条件?
我用我的减速器代码编辑了我的问题。看起来没问题吗? – philk