2017-02-09 23 views
1

我有一個減速,看起來像這樣:mapStateToProps沒有反映在組件動作發出後

export default function reducer(state={ 
    fetching: false, 
}, action) { 
    switch (action.type) { 
    case 'LOGIN_REQUEST': { 
     return {...state, fetching: true} 
    } 
... 
return state 

我從compoenent派遣行動,並在部分我:

const mapStateToProps = (state) => { 
    return state 
} 

我的問題是:
1.在mapStateToProps狀態包含login.fetching: true
2.在組件調整後立即對還原器this.props.login.fetching包含false
3.在組件render()方法this.props.login.fetchingtrue

爲什麼在案件2.它仍然是false,這是可能的,我在這裏失蹤?

行動派遣:

onLoginPress =() => { 
    this.props.dispatch(loginUser(this.state.email.trim(), this.state.password.trim())) 
console.log(this.props); 
+0

您能否特別在您的調度行動中顯示您的組件的代碼 – bencrinkle

+0

更新的問題 – 1110

+2

我不希望在您的調度反映狀態變化後直接看到道具。那隻會出現在重新渲染上,它的聲音就是你所看到的? – bencrinkle

回答

2

答案是非常簡單的,你false右後你dispatch因爲的JavaScript async nature的作用,因此,即使在你props得到修改您的console.log(this.props)被調用,因此它顯示以前的值。然而render()函數在你從reducer改變狀態時被調用,因此它顯示更新的值。

你需要什麼有關於派遣Promise

使用此

onLoginPress =() => { 
    this.props.dispatch(loginUser(this.state.email.trim(), this.state.password.trim())).then(() => { 
    console.log(this.props); 
}) 

我希望,我能正確解釋。

+0

你解釋完美。不知道我可以在派遣上使用諾言。 – 1110

+0

謝謝,很高興有幫助:) –