简体   繁体   中英

Expected an assignment or function call and instead saw an expression no-unused-expressions (lines 14/15)

I am struggling with some code. I keep getting te error above, but I can't seem to solve it. I have looked over it a few times and tried to rewrite lines 14/15, but the problem still persists. Any information on what I am doing wrong would be greatly appreciated.

import React from "react";
import classes from "./Products.module.css";
import Product from "./Product.js";
import apple from "../img/apple.jpg";
import banana from "../img/banana.jpg";
import kiwi from "../img/kiwi.jpg";
import pineapple from "../img/pineapple.jpg";

const Products = props => {
  let productList = Object.keys(props.products)
    .filter(x => {
      props.products[x].quantity > 0;
    })
    .map(x => {
      <Product
        description={props.products[x].description}
        price={props.products[x].price}
        add={() => {
          props.add(x);
        }}
        productCode={x}
        image={apple}
      />;
    });

  return (
    <React.Fragment>
      <div className={classes.Products}>{productList}</div>
    </React.Fragment>
  );
};

export default Products;

You're missing return in the map and filter :

let productList = Object.keys(props.products)
    .filter(x => {
      return props.products[x].quantity > 0;        // Add return here
    })
    .map(x => {
     return  <Product                               // Add return here
        description={props.products[x].description}
        price={props.products[x].price}
        add={() => {
          props.add(x);
        }}
        productCode={x}
        image={apple}
      />;
    });

Alternatively you can remove the braces to have implicit return:

  let productList = Object.keys(props.products)
    .filter(x => props.products[x].quantity > 0)
    .map(x => (
      <Product
        description={props.products[x].description}
        price={props.products[x].price}
        add={() => {
          props.add(x);
        }}
        productCode={x}
        image={apple}
      />
    ));
import React from 'react';
import classes from './Products.module.css';
import Product from './Product.js'
import apple from '../img/apple.jpg';
import banana from '../img/banana.jpg';
import kiwi from '../img/kiwi.jpg';
import pineapple from '../img/pineapple.jpg';



const Products = (props) => {


    let productList = Object.keys(props.products).filter(x=>(props.products[x].quantity>0)).map(x=>(
    <Product description={props.products[x].description} price={props.products[x].price}  add={()=>{props.add(x)}} productCode={x} image ={apple}/>
));

return(
<React.Fragment>
<div className={classes.Products}>
{productList}
</div>
</React.Fragment>
);


}

export default Products;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM