Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/382.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 意外关键字';这';类内组件_Javascript_Reactjs_Next.js - Fatal编程技术网

Javascript 意外关键字';这';类内组件

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

尝试使用Redux saga更新react状态时,我收到一个意外的关键字“this”错误。有人能给我解释一下以下代码的问题是什么,以及如何纠正它吗

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));
    });