Javascript 意外关键字';这';类内组件
尝试使用Redux saga更新react状态时,我收到一个意外的关键字“this”错误。有人能给我解释一下以下代码的问题是什么,以及如何纠正它吗Javascript 意外关键字';这';类内组件,javascript,reactjs,next.js,Javascript,Reactjs,Next.js,尝试使用Redux saga更新react状态时,我收到一个意外的关键字“this”错误。有人能给我解释一下以下代码的问题是什么,以及如何纠正它吗 class Welcome extends React.Component { removeCartItems = (cartItems) => { cartItems.map((product, index) => ({ this.props.dispatch(removeItem(pr
class Welcome extends React.Component {
removeCartItems = (cartItems) => {
cartItems.map((product, index) => ({
this.props.dispatch(removeItem(product)) // Getting error Unexpected keyword 'this'
}));
};
render() {
this.removeCartItems(cartItems)
return (<h1>Remove product from cart</h1>);
}
}
类欢迎扩展React.Component{
RemoveCarItems=(cartItems)=>{
cartItems.map((产品、索引)=>({
this.props.dispatch(removietem(product))//获取意外错误关键字“this”
}));
};
render(){
此。删除部件(cartItems)
退货(从购物车中取出产品);
}
}
您需要删除括号,否则JS会将其解释为您试图从arrow函数返回对象
() => { this. ... }
不是
在您的情况下,它将以以下方式显示
cartItems.map((product, index) => {
this.props.dispatch(removeItem(product));
});
移除{}
周围的()
…?
cartItems.map((product, index) => {
this.props.dispatch(removeItem(product));
});