我正在尝试简单的Redux React测试工作。我对Redux和React都很陌生。我一直在阅读Redux文档,在Troubleshooting部分,它说reducer每次都应该返回新的对象。就我而言,我想我正在回归新对象......
任何建议都表示赞赏!
我的减速机:
const memberOpReducer = (state = {isLoading: true, isLoggedIn: false}, action) => {
switch(action.type) {
case 'CHECK_LOGIN_STATUS':
return {
isLoading: true
}
case 'LOGIN_STATUS':
return {
isLoading: false,
isLoggedIn: action.response.status === 'connected',
response: action.response
};
default:
return state;
}
};
我的行动:
const checkLoginStatus = () => ({
type: 'CHECK_LOGIN_STATUS'
});
const loginStatus = response => ({
type: 'LOGIN_STATUS',
response: response
});
const getLoginStatus = (options = {}) => dispatch => {
dispatch(checkLoginStatus());
FB.getLoginStatus(function (response) {
dispatch(loginStatus(response));
}, options);
}
答案 0 :(得分:1)
问题在于您的mapStateToProps
功能:
// Your function, just for reference
const mapStateToProps = state => {
return {
isLoggedIn: state.isLoggedIn === undefined ? false : state.isLoggedIn,
isLoading: state.isLoading === undefined ? true : state.isLoading,
response: state.response
}
}
您正在访问state.isLoggedIn
,但您应该访问state.memberOpReducer.isLoggedIn
,因为您使用combineReducers
命名空间,该缩减器名称下的该reducer的所有字段。当然,所有其他领域也是如此。
// Fixed version
const mapStateToProps = state => {
return {
isLoggedIn: state.memberOpReducer.isLoggedIn,
isLoading: state.memberOpReducer.isLoading,
response: state.memberOpReducer.response
}
}