react-redux:`state.get` 不是函数
react-redux: `state.get` is not a function
我有如下减速器:
// 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
,我注销了状态对象,它给了我这样的信息:
哪里出了问题?
除非你使用像 ImmutableJS 这样的库,否则 state
只是一个普通的 JavaScript 对象——没有 get
方法可以调用它。您应该只通过点符号访问数据:
const mapStateToProps = (state) => {
console.log(state)
return {
signInForm: state.signInForm
};
};
应该是这样的 state.signInForm
而不是 state.get('signInForm')
我有如下减速器:
// 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
,我注销了状态对象,它给了我这样的信息:
哪里出了问题?
除非你使用像 ImmutableJS 这样的库,否则 state
只是一个普通的 JavaScript 对象——没有 get
方法可以调用它。您应该只通过点符号访问数据:
const mapStateToProps = (state) => {
console.log(state)
return {
signInForm: state.signInForm
};
};
应该是这样的 state.signInForm
而不是 state.get('signInForm')