Fetching data on mount with useEffect
The appointment data we’ll load comes from an endpoint that takes start and end dates. These values filter the result to a specific time range:
GET /appointments/<from>-<to>
Our new component is passed a today
prop that is a Date
object with the value of the current time. We will calculate the from
and to
dates from the today
prop and construct a URL to pass to global.fetch
.
To get there, first, we’ll cover a bit of theory on testing the useEffect
hook. Then, we’ll implement a new renderAndWait
function, which we’ll need because we’re invoking a promise when the component is mounted. Finally, we’ll use that function in our new tests, building out the complete useEffect
implementation.
Understanding the useEffect hook
The useEffect
hook is React’s way of running side effects. The idea is that you provide a function that will run each time any of the hook’s dependencies...