I’m using a loader to fetch the ids of several resources and the user can select the ids of the ones they would like to load.
I haven’t found a way to pass new arguments to the loader to GET new data after the page is loaded.
Created a Stack Overflow account specifically because of this. Would super appreciate any help!!
I’ve found that remix loaders can only accept request, params, and context.
One possible solution may be passing params through the URL but I’m hoping to find a different solution because I need to be able to pass an array of ids. I also haven’t gotten this solution to work.
Another solution I explored is appending the ids with formState but that wouldn’t reload the page (so the loader wouldn’t make another request with the new params).
2
Answers
fetching additional data after the page has already been rendered is typically done using client-side JavaScript. You can use various methods and APIs for fetching data asynchronously, such as the Fetch API or Axios.
Here’s a basic example of how you can fetch additional data after the page has rendered in a Remix application using JavaScript:
// Assuming you have a button with id "fetchDataBtn" in your HTML
In this example:
We select the button element with the id fetchDataBtn.
We add an event listener to the button for the ‘click’ event.
When the button is clicked, an asynchronous HTTP request is made to fetch additional data from the specified API endpoint (/path/to/your/api/endpoint).
Once the data is fetched successfully, it’s parsed as JSON.
You can then use the fetched data to update the DOM or perform any other actions as needed.
Make sure to replace /path/to/your/api/endpoint with the actual URL of your API endpoint that provides the additional data you want to fetch.
With Remix, you can use fetchers (
useFetcher
). It supports JSON payloads as well asFormData
. Since you want to send a payload (your array data) in the body, you’ll want to use the POST method which means you’ll need to create anaction
function.https://remix.run/docs/en/main/hooks/use-fetcher