react-redux:`state.get`不是函数

时间:2017-04-27 11:31:41

标签: reactjs react-redux

我有如下的缩减器:

// SignIn.js
const signIn = (state = {
  mobilePhone: {
    signInWay: 'email',
    countryCallingCode: '+65'
  }
}, action) => {
  switch (action.type) {
    case 'SIGN_IN':
      return Object.assign({}, state, action.signInForm);
    default:
      return state;
  }
};

export default signIn;

// UserInput.js
const userInput = (state = {}, action) => {
  switch (action.type) {
    case 'USER_INPUT':
      return Object.assign({}, state, action.kv);
    default:
      return state;
  }
};

export default userInput;

// Then index.js
import { combineReducers } from 'redux';

import userInput from './UserInput';
import signIn from './SignIn';

const gateApp = combineReducers({
  userInput,
  signInForm: signIn
});

export default gateApp;

然后在mapStateToProps,当我尝试

const mapStateToProps = (state) => {
  console.log(state)
  return {
    signInForm: state.get('signInForm')
  };
};

我收到错误:SignIn.js:9 Uncaught TypeError: state.get is not a function,我注销了状态对象,它给了我这样的东西:

enter image description here

出了什么问题?

2 个答案:

答案 0 :(得分:6)

除非您使用像ImmutableJS这样的库,state只是一个普通的JavaScript对象 - 没有get方法可以调用它。您应该只通过点表示法访问数据:

const mapStateToProps = (state) => {
  console.log(state)
  return {
    signInForm: state.signInForm
  };
};

答案 1 :(得分:1)

它应该看起来像state.signInForm而不是state.get('signInForm')