A
A
AHMED_RAPIRA2021-12-12 23:15:38
React
AHMED_RAPIRA, 2021-12-12 23:15:38

Why is there an error when using useMemo?

There is this component:

import React from "react";

import styles from './styles/main.scss';

interface PaginationProps {
  itemsCount: number;
}

const Pagination: React.FC<PaginationProps> = (props) => {
  const { itemsCount } = props;

  const items = React.useMemo(() => {
    return [...Array(itemsCount).keys()].reverse();
  }, [itemsCount]);

  return (
    <ul className={styles['pagination']}>
      {items.map((itemNumber) => {
        if (!itemNumber) {
          return null;
        }

        return (
          <li
            className={styles['pagination__item']}
            key={itemNumber}
          >
            {itemNumber}
          </li>
        );
      })}
    </ul>
  );
};

Pagination.defaultProps = {
  itemsCount: 0,
};

export default Pagination;


When rendering, I get a standard error:
Uncaught Error: Invalid hook call. Hooks can only be called inside of the body of a function component. This could happen for one of the following reasons:


I tried to do the same in the online sandbox - everything works well. What can be wrong?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
AHMED_RAPIRA, 2022-03-19
@AHMED_RAPIRA

I forgot to point out that the problem occurred when using module federation. When I rummaged through the same version of react for all components, the problem disappeared, and other similar

V
Vladimir, 2021-12-13
@Casufi

Check all the places where you call Pagination.
Maybe somewhere you use Pagination () instead. <Pagination
Well, the error itself gives 5 percent of the necessary information, along with the error, you need a stack.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question