2017-04-27 87 views
0

我有減速象下面這樣:反應,終極版:`state.get`不是一個函數

// 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

哪裏出問題了?

+0

您可以嘗試使用'state.signInForm' –

回答

4

除非你使用像ImmutableJS這樣的庫,否則state只是一個普通的JavaScript對象 - 沒有get方法可以調用它。你應該通過點符號訪問數據:

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

哦,哦,非常感謝你,我還以爲'connect'會玩一些魔法,哈哈... – lnshi

+0

@lnshi:Redux一般會試圖變得非常無魔力:) –

0

應該看起來像這樣state.signInForm沒有state.get('signInForm')

相關問題