initialState 設定了,但沒有正確的show 在頁面上
New reducer i want to add
var initialState = {
value:15
}
Action creators
function clickAdd() {
return {
type: 'CLICK_ADD'
}
}
New reducre
function reducreForButtonGroup(state = initialState, action) {
if (typeof state === 'undefined') {
return 0
}
var value;
switch(action.type) {
case 'CLICK_ADD': {
console.log("2");
return {
value: state.value + 1
}
}
default :{
return state
}
}
}
Component
var ButtonGroup = React.createClass({
clickAdd(event) {
this.props.dispatch(clickAdd());
} ,
render() {
const { value } = this.props;
return (
<ButtonToolbar style={{width: 17+ 'em'}} >
<Button id="search" style={{width: 5 + 'em'}}>{value}</Button>
<Button onClick={this.clickAdd} style={{width: 5 + 'em'}}>Create</Button>
</ButtonToolbar>
);
}
});
Mapstatetoprops
function select(state) {
return {
value: state.reducreForButtonGroup.value
}
}
const NewButtonGroup = connect(select)(ButtonGroup);
combineReducers
var rootReducer = Redux.combineReducers({
reducreForButtonGroup,reducreForButtonGroup2
});
var store = Redux.createStore(rootReducer)