无法获取在我的 react-redux 应用程序的选择器中使用不可变 fromJS 定义的状态
Not able to fetch state defined using immutable fromJS in selector for my react-redux app
这是我的 Login
组件选择器文件
import { createSelector } from 'reselect';
const authentication = () => (state) => state.get('login');
const getCurrentAuthData = () => createSelector(
authentication,
(loginState) => loginState.get('currentUser')
);
export {
authentication,
getCurrentAuthData,
};
这是描述 Login
组件状态的 reducer 文件
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] 代码和基本上复制粘贴代码(学习阶段)。
问题解决,果然是语句写错了。
我从
更改了语句
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
这是我的 Login
组件选择器文件
import { createSelector } from 'reselect';
const authentication = () => (state) => state.get('login');
const getCurrentAuthData = () => createSelector(
authentication,
(loginState) => loginState.get('currentUser')
);
export {
authentication,
getCurrentAuthData,
};
这是描述 Login
组件状态的 reducer 文件
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] 代码和基本上复制粘贴代码(学习阶段)。
问题解决,果然是语句写错了。 我从
更改了语句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