开发者问题收集

无法读取 React 中未定义的属性“长度”

2020-11-05
233

我正在尝试制作分页组件。

但是 totalPost={products.length} 不起作用

因为长度未定义。

我如何获取产品的长度?

import React, { useEffect, useState } from "react";
import { useSelector, useDispatch } from "react-redux";
import { getAllProducts } from "../actions/productAction";
import Product from "../components/Product";
import LoadingBox from "../components/LoadingBox";
import MessageBox from "../components/MessageBox";
import Pagination from "../components/Pagination";

HomeScreen.js

const HomeScreen = (props) => {
  const [searchKeyword, setSearchKeyword] = useState("");
  const [sortOrder, setSortOrder] = useState("newest");

  const [currentPage, setCurrentPage] = useState(1);
  const [limit] = useState(2);

  const productList = useSelector((state) => state.productList);
  const { loading, error, products } = productList;

  const dispatch = useDispatch();

  useEffect(() => {
    dispatch(getAllProducts());
  }, [dispatch]);

  const submitHandler = (e) => {
    e.preventDefault();
    dispatch(getAllProducts("", "", "", searchKeyword, sortOrder));
  };

  const sortHandler = (e) => {
    setSortOrder(e.target.value);
    dispatch(getAllProducts("", "", "", searchKeyword, sortOrder));
  };

  const paginate = (page) => {
    setCurrentPage(page);
  };

  const previousClick = () => {
    setCurrentPage(currentPage - 1);
  };

  const nextClick = () => {
    setCurrentPage(currentPage + 1);
  };

  console.log(sortOrder);
  console.log(currentPage);

  return (
    <div>
      <ul className="filter">
        <li>
          <form onSubmit={submitHandler}>
            <input
              name="searchKeyword"
              onChange={(e) => setSearchKeyword(e.target.value)}
            ></input>
            <button type="submit" className="primary">
              Search
            </button>
          </form>
        </li>
        <li>
          <select name="sortOrder" onChange={sortHandler}>
            <option value="newest">new</option>
            <option value="highest">highest</option>
            <option value="lowest">lowest</option>
          </select>
        </li>
      </ul>
      {loading ? (
        <LoadingBox></LoadingBox>
      ) : error ? (
        <MessageBox variant="danger">{error}</MessageBox>
      ) : (
        <div>

          <div className="row center">
            {products.map((product) => (
              <Product key={product._id} product={product}></Product>
            ))}
          </div>
          <div className="row center">
              <Pagination
                previousClick={previousClick}
                nextClick={nextClick}
                totalPost={products.length} /*problem occur*/
                limit={limit}
                paginate={paginate}
                products={products}
              ></Pagination>
          </div>

        </div>
      )}
    </div>
  );
};

export default HomeScreen;

产品 在此处输入图片说明

在此处输入图片说明

productController.js 在此处输入图片说明

productAction.js 在此处输入图片说明

productReducer.js 在此处输入图片说明

我的 github repo : https://github.com/k3kys/shop3

1个回答

在 getAllProductsReducer 中,您的 products 在某些情况下可能未定义。当您收到 PRODUCT_LIST_REQUESTPRODUCT_LIST_FAIL 时,您的产品将未定义(您未在返回的对象中设置 products

只需检查未定义或默认为空数组:

  const { loading, error, products = [] } = productList;
hiddenuser.2524
2020-11-05