无法在我的react-redux应用程序的选择器中获取使用不可变fromJS定义的状态

时间:2018-02-12 11:16:03

标签: reactjs redux react-redux immutable.js react-boilerplate

这是我Login组件

的选择器文件
import { createSelector } from 'reselect';
const authentication = () => (state) => state.get('login');
const getCurrentAuthData = () => createSelector(
  authentication,
  (loginState) => loginState.get('currentUser')
);
export {
  authentication,
  getCurrentAuthData,
}; 

这是reducer文件,它描述了Login组件

的状态
import { fromJS } from 'immutable';
import { loginConstants } from './constant';

let user = JSON.parse(localStorage.getItem('user'));
const initialState = user ? fromJS({ loggedIn: true, user }) : fromJS({});

function loginReducer(state = initialState, action) {
   switch (action.type) {
   case loginConstants.LOGIN_REQUEST:
       return state
          .set('loggingIn', true)
          .set('user', action.true)
   case loginConstants.LOGIN_SUCCESS:
       return state
          .set('loggedIn', true)
          .set('user', action.true)
  case loginConstants.LOGIN_FAILURE:
       return fromJS({});
  case loginConstants.LOGOUT_REQUEST:
       return fromJS({});
  case loginConstants.LOGOUT_SUCCESS:
       return fromJS({});
  case loginConstants.LOGOUT_FAILURE:
       return fromJS({});
  default:
       return state
  } 
 }

 export default loginReducer;

现在问题是它给出了错误,因为loginState.get不是函数。

PS我引用了React-boilerplate [https://github.com/react-boilerplate/react-boilerplate]代码,基本上是复制粘贴代码(学习阶段)。

1 个答案:

答案 0 :(得分:0)

问题已经解决,正如预期的那样,在编写语句时出错了。 我改变了声明

const authentication = () => (state) => state.get('login');
//here it is returning a function to authentication

const authentication = (state) => state.get('login');
//here it is returning `state.get('login')` to authentication