我有這個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