我有登錄頁面組件,其中我有一個布爾狀態稱爲isUserAuthenticated,當我點擊登錄按鈕,檢查userslist是否用戶是否存在。用戶列表在signupreducer中,我如何訪問loginreducer中的用戶列表並從中返回isUserAuthenticated標誌。如何訪問其他reducer減速器的狀態減少
LoginPage組件
export const mapStateToProps = (state) => {
return {
isUserAuthenticated: state.reducer.isAuthenticated.isUserAuthenticated,
users: state.SignupReducer.users
}
}
export const mapDispatchToProps = (dispatch) => {
return {
checkLogin: (credentials) => dispatch({ type: 'CHECK_LOGIN', credentials })
}
};
LoginPage = connect(mapStateToProps, mapDispatchToProps)(LoginPage);
export default LoginPage;
登錄減速
const initialStateAuth = {
isUserAuthenticated: false
};
export const isAuthenticated = (state = initialStateAuth, action) => {
switch (action.type) {
case 'CHECK_LOGIN':
return Object.assign({}, state, { isUserAuthenticated: checkLogin(state, action) })
default:
return state;
}
}
這裏其實我想檢查用戶在 singupReducer.users我怎麼可以在這裏訪問....
const checkLogin = (state, action) => {
let isExist = false;
signupReducer.users.map((each) => {
if (each.userName === action.credentials.username && each.password === action.credentials.password) {
isExist = true;
}
});
return state.isUserAuthenticated = false;
}
看來你有拼寫問題。有時你使用'singupReducer'或'SignupReducer'。 –
@DamienGold有史以來什麼,但我只用在一個地方,這不是我剛纔提到的「signupReducer」。我的問題是我怎樣才能訪問其他reducer狀態在特定的reducer。謝謝 – kumar