How to return a closure from an async function and assign it to useState?
The problem is, that set_space
doesn't handle functions the same way as other values.
That's because React's state setters can take a callback that receives the previous state as an argument.
However, React has no way of differentiating between a callback and a value that happens to be a function. So, React will assume it's a callback and calls it, then set the state to contain its return value, another Promise.
To put the function itself into the state, you can create a callback that returns the function, like this:
API.SERVER(query, PAGE_SIZE)
.then(response => set_space(() => response));