Line 0: Parsing error: Cannot read property 'map' of undefined" - TypeScript Custom Hook

1.1k Views Asked by At

I have got an error "./src/hooks/usePagination.tsx Line 0: Parsing error: Cannot read property 'map' of undefined" Do you know what is the reason? I spent half the night looking for the answer on stackoferflow etc and had many unsuccessful attempts to fix that.

First I thought about my data format, because now it is

[
    {"id":1,"firstName":"Cori","lastName":"Wescott","email":"[email protected]","gender":"Male","ip_address":"236.58.118.85"},
    {"id":2,"firstName":"Teena","lastName":"Kedge","email":"[email protected]","gender":"Female","ip_address":"74.32.179.20"},
    {"id":3,"firstName":"Englebert","lastName":"Menlove","email":"[email protected]","gender":"Male","ip_address":"91.249.51.126"},
]

and I changed data instead of json, but it didn't help. Later I have looked for a wrong mark, letter, character, whatever - no results. Finally, my research pushed me to reading about too old version of npm or other package. Eh I give up now..

Maybe somebody will find a problem.

function App() {
  const [paginationState, paginationActions] = usePagination(dataEntries, 2);
  return (
    <div className="App">
      <h2>Users</h2>
      { !paginationState.isBusy && <PaginatedTable dataEntries={paginationState.entries}/> }
      <Pagination state={paginationState} actions={paginationActions}/>
    </div>
  );
}

export default App;
d.ts file

export interface User {
  id: number
  firstName: string
  lastName: string
}

export interface PaginationState<T> {
  lastPageIdx: number
  actualPageIdx: number
  entries: T[]
  isBusy: boolean
}

export interface PaginationActions {
  goToFirstPage: () => void
  goToPrevPage: () => void
  goToNextPage: () => void
  goToLastPage: () => void
  goToPage: (number: number) => void
}

custom hook

function usePagination<T>(dataEntries: T[], elementsOnPage: number): [PaginationState<T>, PaginationActions] {
  const [actualPageIdx, setActualPageIdx] = useState(1)
  const lastPageIdx = Math.ceil(dataEntries.length / elementsOnPage)
  const [isBusy, setIsBusy] = useState(false)

  useEffect(() => {
    setIsBusy(true)
    let timer = setTimeout(() => {
      setIsBusy(false)
      clearTimeout(timer)
    }, 333)

    return () => {
      clearTimeout(timer)
    }
  }, [actualPageIdx])

  const entriesOnSelectedPage = () => {
    const firstEntry = (actualPageIdx - 1) * elementsOnPage
    const lastEntry = firstEntry + elementsOnPage
    return dataEntries.slice(firstEntry, lastEntry)
  }

  const entries = entriesOnSelectedPage()

  const goToFirstPage = () => {
    setActualPageIdx(1)
  }

  const goToLastPage = () => {
    setActualPageIdx(lastPageIdx)
  }

  const goToPage = (page: number) => {
    setActualPageIdx(page)
  }

  const goToPrevPage = () => {
    setActualPageIdx((actualPageIdx) => (actualPageIdx === 1 ? actualPageIdx : actualPageIdx - 1))
  }

  const goToNextPage = () => {
    setActualPageIdx((actualPageIdx) =>
      actualPageIdx === lastPageIdx ? actualPageIdx : actualPageIdx + 1
    )
  }
  
  return [
    { 
      actualPageIdx, 
      lastPageIdx, 
      entries, 
      isBusy 
    },
    {
      goToFirstPage,
      goToPrevPage,
      goToPage,
      goToNextPage,
      goToLastPage,
    },
  ] 
}

Pagination

interface PaginationProps {
  state: PaginationState<User>
  actions: PaginationActions
}

const Pagination: FC<PaginationProps> = ({ state, actions }) => {
  const pageNumbers = []

  for (let i = 1; i <= state.lastPageIdx; i++) {
    pageNumbers.push(i)
  }

  const renderPagination = pageNumbers.map((number) => (
    <button
      key={number}
      onClick={() => actions.goToPage(number)}
      className={
        state.actualPageIdx === number ? styles.actualIdxStyle : styles.paginationStyle
      }
    >
      {number}
    </button>
  ))

  return (
    <div className={styles.paginationContainer}>
      <button onClick={actions.goToFirstPage}>GO TO FIRST</button>
      <button onClick={actions.goToPrevPage} data-testid="goToPrevPage">
        <i className="fas fa-chevron-left"></i>
      </button>
      <div data-testid="goToPageButtons">{renderPagination}</div>
      <button onClick={actions.goToNextPage} data-testid="goToNextPage">
        <i className="fas fa-chevron-right"></i>
      </button>
      <button onClick={actions.goToLastPage}>GO TO LAST</button>
    </div>
  )
}

PaginatedTable

interface PaginatedTableProps {
  dataEntries: User[]
}

const PaginatedTable: FC<PaginatedTableProps> = ({ dataEntries }) => {
  const tableEntry = dataEntries && dataEntries.map(({ id, firstName, lastName }) => (
    <div key={id} className={styles.tableStyle}>
      <p>
        {id}{'. '}
      </p>
      <p>
        {firstName} {lastName}
      </p>
    </div>
  ))
  return (
    <div>
      <div className={`${styles.tableStyle} ${styles.headStyle}`}>
        <p>No.</p>
        <p>Name</p>
      </div>
      <div data-testid="users">{tableEntry}</div>
    </div>
  )
}
1

There are 1 best solutions below

0
On BEST ANSWER

Can confirm what @radicand mentioned in the comments above. The following packages needed to be downgraded to these versions:

"@typescript-eslint/eslint-plugin": "3.9.1",
"@typescript-eslint/parser": "3.9.1",
"typescript": "3.9.2"

Typescript 4.x and the eslint 4.x packages don't work with react-scripts at 3.4.3.