我的容器:
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 :(得分:1)
(从评论中删除空格和格式。)
您的reducer映射到哪里?例如,如果您的reducer被映射如下:
export const rootReducer = combineReducers({
uploading: uploadingReducer,
// etc
})
然后你的mapStateToProps
会(注意state
解构):
const mapStateToProps = ({ uploading }) => ({
isUploading: uploading.isUploading
});
它可能会按预期进行更改,但不是传递布尔值,而是传递减速器中包含的整个状态。