` 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);`
在on click函数上我将toggleDiv动作传递给props但是在点击我的屏幕上的标签时它会抛出错误toggleDiv未定义。状态也会被我想要通过动作和减速器传递的值更新
(动作)
export function toggleDiv(){
return {
type: 'TOGGLE_DIV'
};
}
(减速器) `export function toggleDivReducer(state = {hidden:true},action){ switch(action.type){
case 'TOGGLE_DIV':
return Object.assign({}, state, {hidden: !state.hidden});
default:
return state;
}
}`
答案 0 :(得分:0)
您只导入itemsFetchData
操作,但不导入toggleDiv
,因此当mapDispatchToProps
时,toggleDiv
未定义