应进行赋值或函数调用,而是看到一个表达式no-unused-expressions(第14/15行)

时间:2019-08-12 09:43:50

标签: javascript reactjs

我正在努力处理一些代码。我不断遇到上述错误,但似乎无法解决。我已经查看了几次,并试图重写第14/15行,但是问题仍然存在。任何关于我做错事情的信息将不胜感激。

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;

2 个答案:

答案 0 :(得分:1)

您在<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script> <div id="app"> <span>Vue is:</span> <br> <label>Awesome <input type="radio" :checked="radio === 'Awesome'" value="Awesome" @change="radio = $event.target.value" > </label> <label>Super Awesome <input type="radio" :checked="radio === 'Super Awesome'" value="Super Awesome" @change="radio = $event.target.value" > </label> <hr> <span>Selected: {{radio}}</span> </div>return中缺少map

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

答案 1 :(得分:0)

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;