【发布时间】:2016-11-09 19:30:19
【问题描述】:
我有这个Protected HOC。其目的是仅在用户通过身份验证时呈现其WrappedComponent。否则应该呈现AuthenticateComponent(通常是登录组件)。
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
}
组件的 props 来自一个连接的 redux Container 组件
const AccountContainer = ({ children }) => (
<div>{children}</div>
)
const select = state => state.account
export default connect(select, { refreshUser, logout })(Protected(AccountContainer, LoginContainer))
我的account reducer 看起来像这样:
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 并且因此有效。
我想知道那可能是一种什么样的比赛条件?
【问题讨论】:
标签: reactjs redux react-redux