Angala Cheng
Angala Cheng

Reputation: 479

react reducer initialState didnt show as expexted

var rootReducer = Redux.combineReducers({
    reducreForButtonGroup,reducreForButtonGroup2
});

i did set the initialState into the reducre as below, but the Detail button didnt disabled when i load the page .

var initialState2 = {
        disabled:false
        }

function reducreForButtonGroup2(state = initialState2, action) {

}

var DetailButton = React.createClass({ 
            clickDisable(event) {  
                this.props.dispatch(clickDisable());  
            }  ,

          render() {

            const { disable } = this.props;   

            return (
            <ButtonToolbar style={{width: 17+ 'em'}}>
            <Button  disabled={disable} style={{width: 5 + 'em'}}>Detail</Button>
            <Button onClick={this.clickDisable} style={{width: 5 + 'em'}}>Close</Button>
            </ButtonToolbar>)
          }
        }) ;


function select(state) {  
    return {  
        disabled: state.disabled
    }  
}  

const NewDetailButton = connect(select)(DetailButton);

New reducer


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 i did use the reducre name in the mapStateToProps

function select(state) {  
    return {  
        value: state.reducreForButtonGroup.value
    }  
}  

const NewButtonGroup = connect(select)(ButtonGroup);

Upvotes: 4

Views: 51

Answers (1)

Kevin Amiranoff
Kevin Amiranoff

Reputation: 14468

you have spelling issue, it should be const { disabled } = this.props; and disabled={disabled} and I believe it should work.

Also log out what you get from the state because I believe it should be:

function select(state) {  
   console.log(state);
    return {  
        disabled: state.reducreForButtonGroup2.disabled
    }  
} 

One more think it is called a reducer not a reducre. You have it misspelled a few times.

Upvotes: 1

Related Questions