2016-08-20 73 views
1

我有这个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] 

回答

0

,如果你打开控制台,会出现错误

addComponentAsRefTo(...):只有ReactOwner可以有裁判。你可能 增加一个裁判,这不是一个 组件的render方法内创建一个组件,或者你有多个副本的阵营 加载

解决这个问题,你应该https://cdnjs.cloudflare.com/ajax/libs/react/15.3.1/react-with-addons.min.jshttps://cdnjs.cloudflare.com/ajax/libs/react/15.3.1/react.js之间进行选择,因为React应该添加到页面一次。

之后,您需要添加来自react-redux的Provider。您还需要在列表中添加关键道具。

改变笔http://codepen.io/anon/pen/dXLQLv?editors=0010

+0

我知道这是愚蠢的东西...谢谢! – goldbullet

相关问题