I'm helping another developer by adding pagination to a table view of search results. I am new to ReactJS, but am very familiar with vanilla JavaScript. I borrowed code from a ReactJS course, which works fine at home using only Node as the server. At work, we are using Node, Next and PM2.
I'm calling an event handler from an onClick event. The event handler, see below, calls this.setState, but when clicked, I get the error: TypeError: this is undefined
I have tried an arrow function and a normal function. The normal function throws the "undefined" error and the arrow function throws the compile error: "eventHandler is not defined". I've searched here and done a Google search, and I can't find the solution to this problem. There is no class in this component, just some const
function handlePageChange(page){
this.setState((page)=>({currentPage: page}));
}
const Pagination = (props)=>{
const {itemsCount, pageSize}=props;
const pagesCount = Math.ceil(itemsCount / pageSize);
if (pagesCount===1) return null;
let active =1;
const pages = _.range(1,pagesCount+1);
return (
<nav>
<ul className="pagination">
{pages.map(page=>(
<li key={page} active={page===active}>
<a onClick={()=>handlePageChange(page)}>
{page}
</a>
</li>
))}
</ul>
</nav>
)
}
Const Demo = (props)=>{
return (
// ... div, container, table, then the pagination:
<Pagination
itemsCount={props.data.length}
pageSize={pageSize}
currentPage={page}
/>
)}
async function getInitialProps(){}
export default Demo
The pagination displays properly when search results are returned. For example, I get 1 and 2 if there are two pages of results. But when I click on either number, I get TypeError: this is undefined on the this.setState line.