1
我正在使用react和redux,我想在反應視圖中更新我的計數器值,我能夠控制我的redux存儲庫的最新狀態,但它不反映在我的反應視圖中。反應組件不呈現來自REDX存儲區的新數據
const counter = (state = 0, action) => {
console.log(action);
if(action.type == "INCREMENT")
return state + 1;
if(action.type == "DECREMENT")
return state - 1;
else
return state; // return same state if action not identified
}
const {createStore} = Redux;
const store = createStore(counter);
class Counter extends React.Component {
constructor() {
super();
}
render() {
return (
<div>
<div>{this.props.state.getState()}</div>
<button onClick={this.props.onIncrement} >INC</button>
<button onClick={this.props.onDecrement} >DEC</button>
</div>
);
}
}
const render =() => {
ReactDOM.render(
<Counter
state={store}
onIncrement={
() => store.dispatch({ type : "INCREMENT" })
}
onDecrement={
() => store.dispatch({ type : "DECREMENT" })
}
/>,
document.querySelector('#counter'));
}
store.subscribe(function() {
console.log(store.getState())
});
render();
你的反應組件如何與redux狀態耦合?您無法直接訪問作爲反應組件中的道具的redux狀態。 – Hareesh
使用react-redux綁定您的反應應用程序http://redux.js.org/docs/basics/UsageWithReact.html –