iT邦幫忙

0

react reducer 設定mapStateToProps 問題

  • 分享至 

  • xImage

initialState 設定了,但沒有正確的show 在頁面上/images/emoticon/emoticon02.gif

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)
Homura iT邦高手 1 級 ‧ 2018-08-14 14:52:48 檢舉
你的程式碼有給完整嗎?@@
buttontoolbar是哪來的組件...
reactstrap?
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友回答

立即登入回答