我可以使用动作和减速器更新状态,但是当我尝试对它抛出的动作执行 onClick 时在组件上
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);`
在点击功能上,我将 toggleDiv 操作传递给 props,但在点击我屏幕上的标签时,它会抛出错误 toggleDiv not defined.The state is also getting updated by the value which I want to pass using动作和减速器
(动作)
export function toggleDiv(){
return {
type: 'TOGGLE_DIV'
};
}
(减速器)
`导出函数 toggleDivReducer(state = { hidden: true}, action){
开关(action.type){
case 'TOGGLE_DIV':
return Object.assign({}, state, {hidden: !state.hidden});
default:
return state;
}
}`
您只导入 itemsFetchData
操作而不导入 toggleDiv
,所以当 mapDispatchToProps
时,toggleDiv
不是未定义的
` 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 操作传递给 props,但在点击我屏幕上的标签时,它会抛出错误 toggleDiv not defined.The state is also getting updated by the value which I want to pass using动作和减速器
(动作)
export function toggleDiv(){
return {
type: 'TOGGLE_DIV'
};
}
(减速器) `导出函数 toggleDivReducer(state = { hidden: true}, action){ 开关(action.type){
case 'TOGGLE_DIV':
return Object.assign({}, state, {hidden: !state.hidden});
default:
return state;
}
}`
您只导入 itemsFetchData
操作而不导入 toggleDiv
,所以当 mapDispatchToProps
时,toggleDiv
不是未定义的