Fetch stuff ...with optional parameters

Here's a button.

This one should return 17 items.

Here's another button.

This one should return 2 items.

navigate my process

related content

The Explanation

Getting one file over and over is boring. What about including some optional parameters in our fetch() request? For that I'm using the, optional, uh, options parameter.

For this I've set up a truly basic mock api endpoint on Netlify, which I'm not going to get into other than to say all it does is grabs a data array and slices off the requested number of items from it. So then my front end buttons can be wired up to request fixed but distinct numbers of items, using the body parameter of the requestOptions object., which body parameter is a JS options object passed through JSON.stringify().

Note that in this case we're using a POST request, which is mostly because that's how I have my Netlify function set up? For that bit I mostly just grabbed some code I had lying around and tossed it in to make this work. But the flexibility should be there for using, say, a GET request, or whatever.