[英]React-Redux connect() not updating component even when the state is changed without mutation
I have this codepen , where store.subscribe()
works and connect()
doesn't work. 我有这个codepen ,其中
store.subscribe()
工作, connect()
不起作用。 Specifically, the component doesn't get updated with the new props. 具体来说,组件不会使用新的道具进行更新。 I suspected state mutation as I thought that connect()'s shallow equality check might be ignoring the change.
我怀疑状态突变,因为我认为connect()的浅层等式检查可能忽略了变化。 But, I'm using
Immutable.js
for the state change in the reducer, and I also did my own ref check in my subscribe method and it is even shallowly different for every update. 但是,我正在使用
Immutable.js
进行reducer中的状态更改,并且我也在我的subscribe方法中进行了自己的ref检查,并且每次更新时它都会有很小的不同。 I feel like something obvious must be missing here... 我觉得这里肯定有一些显而易见的东西......
Component: 零件:
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);
Reducer: 减速器:
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;
}
};
Store: 商店:
const store = createStore(combineReducers({ alerts: alertsReducer }), applyMiddleware(ReduxThunk.default));
Render: 渲染:
//** 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();
Is this because the top level state object still has the same reference? 这是因为顶级状态对象仍然具有相同的引用吗? I tried removing
Immutable.js
and made the entire state the array with the reducer returning a new array every time. 我尝试删除
Immutable.js
并使整个状态使用reducer每次都返回一个新数组。 That still didn't work. 那还是行不通的。
Versions: 版本:
react-redux@4.4.5
redux@3.5.2
react@15.3.1
if you open the console , there will be the error 如果你打开控制台,就会出现错误
addComponentAsRefTo(...): Only a ReactOwner can have refs.
addComponentAsRefTo(...):只有ReactOwner可以有refs。 You might be adding a ref to a component that was not created inside a component's
render
method, or you have multiple copies of React loaded您可能正在向组件的
render
方法中未创建的组件添加ref,或者您有多个React副本已加载
to solve it you should choose between https://cdnjs.cloudflare.com/ajax/libs/react/15.3.1/react-with-addons.min.js and https://cdnjs.cloudflare.com/ajax/libs/react/15.3.1/react.js , because React should be added to page once. 要解决这个问题,你应该在https://cdnjs.cloudflare.com/ajax/libs/react/15.3.1/react-with-addons.min.js和https://cdnjs.cloudflare.com/ajax/libs之间做出选择。 /react/15.3.1/react.js ,因为React应该被添加到页面一次。
after that you need to add Provider from react-redux. 之后,您需要从react-redux添加Provider。 Also you need to add key props in your lists.
您还需要在列表中添加关键道具。
changed pen http://codepen.io/anon/pen/dXLQLv?editors=0010 更改了笔 http://codepen.io/anon/pen/dXLQLv?editors=0010
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.