React-Router
To fetch data using the React-Router Route components loader prop, you first need to install React-Router to your project. Once installed, use the loader prop to fetch data and select the code input.
Make your fetch call.
(note: you can make any data request with the React-Router loader)
export default (props, vars) => async ({params}) => {
// write your code here
fetch("https://swapi.dev/api/planets/2/")
.then(response => response.json())
.then(data => console.log(data))
}
You can then use any data returned from the loader prop using the UseLoaderData component from the React-Router package. You will need to change the fetch call in your loader prop to return the data rather than log it.
To learn more:
React-Router and the Route component loader prop.
Updated about 2 months ago