skip to Main Content

I have three components:

  1. Parent (HomePage.jsx)
export default function HomePage() {
  const [currentSorting, setCurrentSorting] = useState();

  return (
    <>
      <IntroSlider />

      <main className="main-content">
        <MainContentHeader setSorting={setCurrentSorting} />

        <ProductSection currentFuncOfSorting={currentSorting} />
      </main>
    </>
  );
}
  1. Child(ProductSection.jsx)
export default function ProductSection({ currentFuncOfSorting }) {
  const [limitedProductList, setProductsToShow] = useState([]);
  const [next, setNext] = useState(initialCountProducts);
  const [sortProductList, setSortProductList] = useState(productList);

  useEffect(() => {
    let sorted = [...sortProductList.sort(currentFuncOfSorting)];
    setSortProductList(sorted);
    setProductsToShow(sorted.slice(0, initialCountProducts));
  }, [currentFuncOfSorting]);

  const loopWithSlice = (start, end) => {
    const slicedProduct = sortProductList.slice(start, end);
    setProductsToShow(slicedProduct);
  };

  const handleShowMorePosts = () => {
    loopWithSlice(0, next + productsPerСlick);
    setNext(next + productsPerСlick);
  };

  return (
    <>
      <section className="product-section">
        {limitedProductList.map((item, index) => (
          <ProductCard key={index} {...item} />
        ))}
      </section>
      {limitedProductList.length < productList.length && (
        <div className="product-section__button">
          <button onClick={handleShowMorePosts}>Upload more</button>
        </div>
      )}
    </>
  );
}
  1. Child(MainContentHeader.jsx)
...
<select
  onChange={(e) => {
    switch (e.target.value) {
      case "price:asc":
        ()=>setSorting(SortPriceAsc);
        break;
      case "price:desc":
        ()=>setSorting(SortPriceDesc);
        break;
    }
  }}
  className="main-content-header__order"
>
  <option data-filter-value="" value="">
    Order: Default
  </option>
  <option data-filter-value="price:asc" value="price:asc">
    Price: ascending
  </option>
  <option data-filter-value="price:desc" value="price:desc">
    Price: descending
  </option>
</select>
...

SortPriceAsc and SortPriceDesc are sorting functions

I’m trying to pass a state change function to MainContentHeader.jsx. And when you change it, I want the type of product sorting to change in ProductSection.jsx.

However, the code is not working, and in search of a solution, if I understood correctly, this is a bad way to solve the problem.

2

Answers


  1. Chosen as BEST ANSWER

    The problem has been solved. I hope I understood correctly, only functions that do not accept arguments can be added to state, so we turn the sorting function into an arrow function.

    If I'm wrong, correct me.

    <select
      onChange={(e) => {
        switch (e.target.value) {
          case "price:asc":
            setSorting(() => SortPriceAsc);
            break;
          case "price:desc":
            setSorting(() => SortPriceDesc);
            break;
          ...
          default:
            setSorting(() => DefaultSortIdAsc);
        }
      }}
      className="main-content-header__order"
    >
      <option data-filter-value="" value="">
        Order: Default
      </option>
      <option data-filter-value="price:asc" value="price:asc">
        Price: ascending
      </option>
      <option data-filter-value="price:desc" value="price:desc">
        Price: descending
      </option>
      ...
    </select>
    

  2. <select
        onChange={(e) => {
            switch (e.target.value) {
               case "price:asc":
                   ()=>setSorting(SortPriceAsc);
               break;
               case "price:desc":
                   ()=>setSorting(SortPriceDesc);
               break;
            }
        }}
        className="main-content-header__order"
    >
    

    in the onChange event you are handling the event using a switch case but while calling the setter for sorting you are using an arrow function which is not required. You can just call setSorting function to set the value.

    i.e.

    <select
         onChange={(e) => {
            switch (e.target.value) {
               case "price:asc":
                   setSorting(SortPriceAsc);
               break;
               case "price:desc":
                   setSorting(SortPriceDesc);
               break;
        }}} className="main-content-header__order"
    >
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search