我有这个codepen,其中store.subscribe()
工作和connect()
不起作用。具体来说,组件不会更新新的道具。我认为connect()的浅平等检查可能会忽略这种变化,所以我怀疑状态变异。但是,我正在使用Immutable.js
来处理reducer中的状态变化,并且我也在我的订阅方法中进行了自己的ref检查,并且每次更新都会发生很小的差异。我觉得像明显的东西必须在这里失去了...React-Redux connect()不更新组件,即使当状态改变时没有突变
组件:
class App extends React.Component{
...
}
const mapStateToProps = state => ({
alerts: state.alerts
});
const mapDispatchToProps = dispatch => ({
onAlert: (type, autoHide, message) =>
dispatch({ type: 'SHOW_ALERT', payload: { message, type, autoHide } })
});
const ConnectedApp = connect(mapStateToProps, mapDispatchToProps)(App);
减速机:
const alertsReducer = (alerts=Immutable.List(), { type, payload }) => {
switch (type){
case 'SHOW_ALERT':
if (!payload.message || R.trim(payload.message).length === 0){
throw new Error('Message cannot be empty.');
}
return alerts.push(payload);
default:
return alerts;
}
};
商店:
const store = createStore(combineReducers({ alerts: alertsReducer }), applyMiddleware(ReduxThunk.default));
渲染:
//** THIS DOESN'T WORK
// ReactDOM.render(<Provider store={store}><ConnectedApp /></Provider>, document.getElementById('main'));
//** THIS WORKS
store.subscribe(()=>{
render();
});
const render =() => {
ReactDOM.render(<App {...store.getState()} onAlert={
(type, autoHide, message) => store.dispatch({ type: 'SHOW_ALERT', payload: { message, type, autoHide } })
}/>, document.getElementById('main'));
};
render();
这是因为顶级状态对象仍然具有相同的引用吗?我尝试删除Immutable.js
,并使整个状态的数组与reducer每次都返回一个新的数组。这仍然没有奏效。
版本:
[email protected]
[email protected]
[email protected]
我知道这是愚蠢的东西...谢谢! – goldbullet