1

I'm trying to make pagination components.

but totalPost={products.length} is not working

because length is undefined.

how can i get length of products ?

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;

products enter image description here

enter image description here

productController.js enter image description here

productAction.js enter image description here

productReducer.js enter image description here

my github repo : https://github.com/k3kys/shop3

1
  • Can you try: products && products.length. Commented Nov 5, 2020 at 11:11

1 Answer 1

1

In your getAllProductsReducer your products might be undefined in certain cases. When you receive a PRODUCT_LIST_REQUEST or PRODUCT_LIST_FAIL you products will be undefined (you didn't set products in the returned object)

Just check for undefined as well or default to an empty array:

  const { loading, error, products = [] } = productList;
Sign up to request clarification or add additional context in comments.

2 Comments

it takes 3 hours T_T... but you solve this only 1min...
glad I could help, you can mark the answer if this helped you

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.