So I'm building a React application that is making API calls to receive a random object containing a quote, author, and some other information. When the component mounts(on page refresh) the object is randomized. However, the button bound to the same function that initially pulls the API only receives the same object as when the page loaded. The randomization is provided from the endpoint and not within my code.
Not sure why this wouldn't work as the data received is unique on page refresh, but not on the onClick event.
So here's some of my code that I'm using:
My API fetch function:
httpCallout(){
fetch('https://quotesondesign.com/wp-json/posts?filter[orderby]=rand&filter[posts_per_page]=1')
.then(response => response.json())
.then((data) => {
const quote = data[0].content;
this.setState({
quote: quote,
author: data[0].title
});
});
}
The call within the lifecycle function:
componentDidMount(){
this.httpCallout();
}
and then the element
<button id="new-quote" className="btn btn-light" onClick={this.httpCallout}>New Quote</button>
So again, I'm expecting to be receiving a new object containing new data on each click event, however, I'm being fed the same data as on the page load.
EDIT:
It was simply data being cached. I added {cache: "no-cache"}
into the fetch request and it was solved. The question has been marked as a duplicate, however I find the solution required to be slightly different since my issue was using the fetch API and not using jQuery or AJAX.