[英]I am able to update the state using action and reducer but on the component when I try to do onClick for the action it throws
` 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 the on Click function I am passing the toggleDiv action to props but on clicking the tag on my screen it throws error toggleDiv not defined.The state is also getting updated by the value which I want to pass using action and reducer (action) 在on Click函数上,我正在将toggleDiv动作传递给道具,但是单击我的屏幕上的标签时,它会抛出未定义的错误toggleDiv。状态也被我要使用动作和reducer(动作)传递的值更新
export function toggleDiv(){ return { type: 'TOGGLE_DIV' }; }
(reducer) `export function toggleDivReducer(state = { hidden: true}, action){ switch(action.type) { (reducer)`导出功能toggleDivReducer(state = {hidden:true},action){switch(action.type){
case 'TOGGLE_DIV':
return Object.assign({}, state, {hidden: !state.hidden});
default:
return state;
}
}`
您只导入itemsFetchData
操作,而不是toggleDiv
,因此当mapDispatchToProps
, toggleDiv
不会是未定义的
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.