我是新来的REDX - 为什么mapStateToProps
被调用,组件更新显示'你好世界'?还原 - 反应简单你好世界
http://codepen.io/anon/pen/QyXyvW?editors=0011
const helloReducer = (state= {message:'none'}, action) => {
switch (action.type) {
case 'HELLO':
return Object.assign(state,{message:"hello world"});
default:
return state;
}
};
const myApp = Redux.combineReducers({
helloReducer
});
const App = ({onClick,message}) => (
<div>
<a href="#" onClick={onClick}>click</a><b>{message}</b>
</div>
);
const mapStateToProps = (state, ownProps) => {
return {message: state.message}
};
const mapDispatchToProps = (dispatch, ownProps) => {
return {
onClick:() => {
dispatch({type: 'HELLO'})
}
}
}
const ConnectedApp = ReactRedux.connect(
mapStateToProps,
mapDispatchToProps
)(App);
let Provider = ReactRedux.Provider;
let store = Redux.createStore(myApp)
let e = React.render(
<Provider store={store}>
<ConnectedApp />
</Provider>,
document.getElementById('root')
);
现在很棘手。我在尝试调试问题时错过了它。这是使用es6传播运算符'return {... state,{message:“hello”};'在我看来这更明显且可读的这里的潜在好理由。 –
是的,变异状态是“为什么我的组件没有更新?”的第一个答案,这就是为什么它会进入我正在处理的Redux FAQ页面。 – markerikson