이 항목이 Protected
HOC입니다. 그 목적은 사용자가 인증 될 때만 WrappedComponent
을 렌더링하는 것입니다. 그렇지 않으면 AuthenticateComponent
을 렌더링해야합니다 (일반적으로 로그인 구성 요소). 구성 요소에 대한감기 걸기 상태가 계속 표시되면 redux 상태가 표시되지 않아야 함
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
}
소품은
const AccountContainer = ({ children }) => (
<div>{children}</div>
)
const select = state => state.account
export default connect(select, { refreshUser, logout })(Protected(AccountContainer, LoginContainer))
내 account
감속기는 다음과 같습니다 연결된 REDUX 컨테이너 구성 요소에서 오는 :
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