[英]Redux not updating components when deep Immutable state properties are updated
我的问题:为什么不在我的不可变状态(Map)中更新数组中对象的属性不会导致Redux更新我的组件?
我正在尝试创建一个将文件上传到我的服务器的小部件,我的初始状态(从我将在下面看到的UploaderReducer内部)对象看起来像这样:
let initState = Map({
files: List(),
displayMode: 'grid',
currentRequests: List()
});
我有一个thunk方法,可以在事件发生时启动上传和调度操作(例如进度更新)。 例如,onProgress事件如下所示:
onProgress: (data) => {
dispatch(fileUploadProgressUpdated({
index,
progress: data.percentage
}));
}
我正在使用redux-actions
来创建和处理我的动作,所以我对该动作的reducer看起来像这样:
export default UploaderReducer = handleActions({
// Other actions...
FILE_UPLOAD_PROGRESS_UPDATED: (state, { payload }) => (
updateFilePropsAtIndex(
state,
payload.index,
{
status: FILE_UPLOAD_PROGRESS_UPDATED,
progress: payload.progress
}
)
)
}, initState);
并且updateFilePropsAtIndex
看起来像:
export function updateFilePropsAtIndex (state, index, fileProps) {
return state.updateIn(['files', index], file => {
try {
for (let prop in fileProps) {
if (fileProps.hasOwnProperty(prop)) {
if (Map.isMap(file)) {
file = file.set(prop, fileProps[prop]);
} else {
file[prop] = fileProps[prop];
}
}
}
} catch (e) {
console.error(e);
return file;
}
return file;
});
}
到目前为止,这一切似乎都运行良好! 在Redux DevTools中,它显示为预期的动作。 但是,我的组件都没有更新! 将新项目添加到files
数组会重新呈现我添加了新文件的UI,因此Redux当然没有问题...
我使用connect
连接到商店的顶级组件如下所示:
const mapStateToProps = function (state) {
let uploadReducer = state.get('UploaderReducer');
let props = {
files: uploadReducer.get('files'),
displayMode: uploadReducer.get('displayMode'),
uploadsInProgress: uploadReducer.get('currentRequests').size > 0
};
return props;
};
class UploaderContainer extends Component {
constructor (props, context) {
super(props, context);
// Constructor things!
}
// Some events n stuff...
render(){
return (
<div>
<UploadWidget
//other props
files={this.props.files} />
</div>
);
}
}
export default connect(mapStateToProps, uploadActions)(UploaderContainer);
uploadActions
是一个使用redux-actions
创建redux-actions
的对象。
files
数组中的file
对象基本上是这样的:
{
name: '',
progress: 0,
status
}
UploadWidget
基本上是一个拖放div和一个打印在屏幕上的files
数组。
我尝试使用redux-immutablejs
来帮助我,就像我在GitHub上的许多帖子中看到的那样,但我不知道它是否有帮助...这是我的根减速器:
import { combineReducers } from 'redux-immutablejs';
import { routeReducer as router } from 'redux-simple-router';
import UploaderReducer from './modules/UploaderReducer';
export default combineReducers({
UploaderReducer,
router
});
我的app入口点如下所示:
const store = configureStore(Map({}));
syncReduxAndRouter(history, store, (state) => {
return state.get('router');
});
// Render the React application to the DOM
ReactDOM.render(
<Root history={history} routes={routes} store={store}/>,
document.getElementById('root')
);
最后,我的<Root/>
组件如下所示:
import React, { PropTypes } from 'react';
import { Provider } from 'react-redux';
import { Router } from 'react-router';
export default class Root extends React.Component {
static propTypes = {
history: PropTypes.object.isRequired,
routes: PropTypes.element.isRequired,
store: PropTypes.object.isRequired
};
get content () {
return (
<Router history={this.props.history}>
{this.props.routes}
</Router>
);
}
//Prep devTools, etc...
render () {
return (
<Provider store={this.props.store}>
<div style={{ height: '100%' }}>
{this.content}
{this.devTools}
</div>
</Provider>
);
}
}
因此,最终,如果我尝试更新以下状态对象中的“进度”,则React / Redux不会更新我的组件:
{
UploaderReducer: {
files: [{progress: 0}]
}
}
为什么是这样? 我认为使用Immutable.js的整个想法是,无论您更新它们有多深,都比较容易比较修改过的对象?
看起来一般使用Redux与Redux一起工作并不像看起来那么简单: 如何在REDx中使用Immutable.js? https://github.com/reactjs/redux/issues/548
然而,使用Immutable的吹捧好处似乎值得这场战斗,我很想知道我做错了什么!
更新2016年4月10日选定的答案告诉我我做错了什么,为了完整起见,我的updateFilePropsAtIndex
函数现在只包含这个:
return state.updateIn(['files', index], file =>
Object.assign({}, file, fileProps)
);
这非常有效! :)
两个一般的想法第一:
现在,假设你正在使用Immutable.js,我承认数据的突变似乎有点不太可能。 那说...你的reducer中的file[prop] = fileProps[prop]
行似乎非常好奇。 你究竟想要去那里的是什么? 我会好好看看那一部分。
实际上,现在我看着它......我几乎100%肯定你在改变数据。 您的updater回调到state.updateIn(['files', index])
将返回与参数完全相同的文件对象。 根据https://facebook.github.io/immutable-js/docs/#/Map上的Immutable.js文档:
如果updater函数返回与调用的值相同的值,则不会发生任何更改。 如果提供notSetValue,则仍然如此。
是的。 你返回的是你给出的相同值,你的直接突变出现在DevTools中,因为那个对象仍然在闲逛,但是因为你返回了同一个对象,所以Immutable.js实际上并没有进一步返回任何修改过的对象层次结构。 因此,当Redux检查顶级对象时,它看到没有任何更改,不会通知订阅者,因此组件的mapStateToProps
永远不会运行。
清理你的减速器并从更新器内部返回一个新对象,它应该都可以正常工作。
(一个相当迟来的答案,但我刚刚看到了这个问题,它似乎仍然是开放的。希望你现在实际上已经解决了......)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.