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

lns*_*shi 1 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;
Run Code Online (Sandbox Code Playgroud)

然后在mapStateToProps,当我试图做

const mapStateToProps = (state) => {
  console.log(state)
  return {
    signInForm: state.get('signInForm')
  };
};
Run Code Online (Sandbox Code Playgroud)

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

在此输入图像描述

哪里出错了?

Joe*_*lay 7

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

const mapStateToProps = (state) => {
  console.log(state)
  return {
    signInForm: state.signInForm
  };
};
Run Code Online (Sandbox Code Playgroud)

  • 如果使用 ImmutableJS 怎么办 - 我在尝试在 react-boilerplate 中启用持久性时遇到此错误 (2认同)