The code utilizes several state variables to manage the application's behavior and data flow:
jobIds
: This variable stores an array of job IDs retrieved from the Hacker News API. It is initially set to null
and later populated with data from the API. It allows for pagination and fetching job details based on the IDs.page
: This variable keeps track of the current page number, determining which set of job IDs and details to fetch. It is incremented when the user clicks on the "Load more" button.Some notable aspects of the rendering code include:
display: grid
CSS property. This is a convenient way to allow consistent spacing between job postings.fetchJobIds
This asynchronous method is responsible for fetching the current page's list of job IDs from the Hacker News API and is called with the current page number as an argument.
Since the API only has a single endpoint to fetch all the top job listings, we only need to fetch the list once and save it as jobIds
in state by making a GET
request to the API endpoint. Once the job IDs are retrieved, it slices the array based on the current page and returns the relevant subset of job IDs. Subsequent calls to this function will just be slicing the array without fetching the data again.
fetchJobs
This asynchronous method fetches the job details based on the job IDs obtained from the fetchJobIds
method. It is called with the current page number as an argument. Inside the method, it calls fetchJobIds(currPage)
to get the job IDs for the current page then calls setFetchingJobDetails
with true
to indicate that job details are being fetched. Using Promise.all
, it makes multiple GET requests to the Hacker News API to fetch the details of each job, using the current page's job IDs. Once the job details are obtained, it appends the new jobs to the bottom of the job list. Finally, it calls setFetchingJobDetails
with false
to indicate that the fetching process is complete.
This function is called when the page first loads and when the "Load more" button is pressed.
url
field in the job details.