2
我試圖增加和發生反應,使用react-redux
。我添加動作,容器,減速。但我不知道怎麼在這裏認購increment
和decrement
行動遞減狀態值是我的代碼如何在反應中增加和減少狀態值?
我想增加和減少值當按鈕
這裏用戶點擊是我的代碼 http://codepen.io/anon/pen/jVjMXv?editors=1010
const abc= (state=0,action) => {
console.log(action.type)
switch(action.type){
case 'INCREMENT':
return state +1
case 'DECREMENT':
return state -1
Default :
return state;
}
}
const {createStore,bindActionCreators} =Redux;
const {Provider,connect} =ReactRedux;
const store = createStore(abc);
class First extends React.Component {
constructor(){
super();
this.state ={
digit :0
}
}
inc(){
console.log('ince')
}
dec(){
console.log('dec')
}
render(){
return (
<div>
<button onClick={this.inc.bind(this)}>INCREMENT</button>
<p>{this.state.digit}</p>
<button onClick={this.dec.bind(this)}>DECREMENT</button>
</div>
)
}
}
const actions = {
increment:() => {
return {
type: 'INCREMENT',
}
},
decrement:() => {
return {
type: 'DECREMENT',
}
}
};
const AppContainer = connect(
function mapStateToProps(state) {
return {
digit: state
};
},
function mapDispatchToProps(dispatch) {
return bindActionCreators(actions, dispatch);
}
)(First);
ReactDOM.render(
<Provider store={store}>
<First/>
</Provider>
,document.getElementById('root'))
我不'First'和'third'清楚了嗎? – user5711656
爲什麼要渲染「集裝箱」 ......所以,如果我有兩個組成部分'(一級,二級)'各有獨立的容器('AppContainer1','AppContainer2')..所以我會在加渲染方法的容器?或只有組件? – user5711656
第三點是明確的..我只有在「第一」點 – user5711656