我能够更新状态使用行动和减速器,但在组件上时,我试图做onClick它的动作

问题描述:

` import React, { Component, PropTypes } from 'react'; 
     import { connect } from 'react-redux'; 
     import { itemsFetchData } from '../actions/sidenavaction'; 

     class SideNavItem extends Component { 
      componentDidMount() { 
      this.props.fetchData('http: 
      //58f5d2ccc9deb71200ceecef.mockapi.io/nav'); 
      } 

      render() {  
      var Nest=function(par) { 
       const numbers = par.itemized; 
       const listItems = numbers.map((number) => <li key= 
      {number.sid}>{number.svalue}</li>); 
       return (<ul>{listItems}</ul>); 
      }; 



      if (this.props.hasErrored) { 
       return <p>Sorry! There was an error loading the items</p>; 
       } 

      if (this.props.isLoading) { 
     return <p>Loading…</p>; 
       } 

      return (
     <ul>{this.props.items.map((item) =>   
     <ul key={item.id} onClick={this.props.toggleDiv}><a href="#"> 
      {item.value}</a> 
      {item.sub && <Nest itemized={item.sub} />}   
     </ul>    
     )} 
     </ul> 
      );   
      } 
      } 

     const mapStateToProps = (state) => { 
     return { 
    items: state.items, 
    hasErrored: state.itemsHasErrored, 
    isLoading: state.itemsIsLoading 
      }; 
      }; 

     const mapDispatchToProps = (dispatch) => { 
       return { 
      fetchData: (url) => dispatch(itemsFetchData(url)), 
       toggleDiv:() => dispatch(toggleDiv()) 
      }; 
     }; 

       export default connect(mapStateToProps, mapDispatchToProps) 
     (SideNavItem);` 

在点击功能,我通过toggleDiv行动道具,但点击标签我的屏幕上,它抛出错误toggleDiv不defined.The状态也过得去,我想用行动和减速器传递的值更新 (动作)
export function toggleDiv(){ return { type: 'TOGGLE_DIV' }; }我能够更新状态使用行动和减速器,但在组件上时,我试图做onClick它的动作

(减速) `导出功能toggleDivReducer(状态= {hidden:true},action){switch(action.type){

 case 'TOGGLE_DIV': 
     return Object.assign({}, state, {hidden: !state.hidden}); 

      default: 
      return state; 

      } 

      }` 
+0

未捕获的ReferenceError:toggleDiv没有定义请解释.THANKSALOT – Enthu

+0

你在导入缺少'toggleDiv'?从'../ actions/sidenavaction'导入{itemsFetchData};'?? – Panther

+0

哦,谢谢你会看看它 – Enthu

你只能导入itemsFetchData作用,但不能toggleDiv,所以当mapDispatchToPropstoggleDiv不是未定义

+0

哦,非常感谢它会检查它 – Enthu

+0

非常感谢它很有用 – Enthu