2016-12-06 33 views
0

我的容器:爲什麼我的狀態在更改後沒有映射到我的屬性?

import React, { Component } from 'react'; 
import { connect } from 'react-redux'; 
import Example from '../components/example.jsx'; 

class ExampleContainer extends Component { 
    render() { 
    return (
     <Example isUploading={this.props.isUploading}/> 
    ); 
    } 
} 

const mapStateToProps = (state) => ({ 
    isUploading: state.isUploading 
}); 

export default connect(mapStateToProps)(ExampleContainer); 

我減速機:

import { combineReducers } from 'redux'; 
import * as actionTypes from '../constants/action-types.js'; 

const initialState = { 
    isUploaded: false, 
    isUploading: false 
}; 


function isUploading(state = initialState, action) { 
    switch (action.type) { 
    case actionTypes.IS_UPLOADED: 
     return action.payload; 
    default: 
     return state; 
    } 
} 

function isUploaded(state = initialState, action) { 
    switch (action.type) { 
    case actionTypes.IS_UPLOADING: 
     return action.payload; 
    default: 
     return state; 
    } 
} 

export default combineReducers({ 
    isUploading, 
    isUploaded, 
}); 

我看到我的isUploading狀態我的控制檯變化,但該財產isUploading上我的例子成分沒有改變。它與第一次定義的屬性具有相同的值。我也看到mapStateToProps函數只被調用一次。

+0

add example.jsx code also –

+0

如何/何時分派更新? – Pineda

回答

1

(從空間和格式註釋感動。)

哪裏是你的減速映射到?例如,如果你的減速映射這樣的:

export const rootReducer = combineReducers({ 
    uploading: uploadingReducer, 
    // etc 
}) 

那麼你mapStateToProps會(注意state解構):

const mapStateToProps = ({ uploading }) => ({ 
    isUploading: uploading.isUploading 
}); 

很可能它的變化不如預期,但不是通過一個布爾值,你'通過減速機中的整個狀態。

+0

但如果我有多個狀態來映射? (原因是什麼,但我刪除它們使這個問題更簡單) –

+0

我已經用我目前的reducer文件更新了我的文章 –

+0

@JimPeeters \t如果?你可以映射任何你想要的,但你仍然必須繪製正確的東西。在不知道你的有效載荷是什麼的情況下,仍然不可能知道代碼是否有意義。 (或者該屬性如何在子組件中使用)。當您在[redux-devtools]中查看它時,狀態如何(https://github.com/gaearon/redux-devtools) ? –

相關問題