Hi all, I am working on moving away from HTTP and start using fetch. Case is a simple external API with a simple list exposed on a url returning some json. Sounds really simple, but I have yet not managed to elegantly integrate this API together with my existing mongo collections.
How do I get the external data to resemble a collection as much as possible, and where do I find some nice updated documentation on how to do it?
Fetch is working fine, but the async nature and the promise based returns are difficult for non-gurus.
Thanks gents, these might lead to a solution, still fiddling. But I am thinking that a Promise based approach would make more sense, but I have tried several things that ends up in problems.I am looking for the best way to do it in the Meteor framework. This should be something that a lot of people are looking for but I cannot find a beautiful example.
convention would be const [data, setData] = useState(null) or const [models, setModels] = useState(null)
Ok, your models starts with a null which basically means loading data is true (you don’t need another local state for it). You then get data and set it into the local state. Having something other than null in models means data loading ended so … it is false.
For every update of the local state, the view re-renders so … you can just remove loadingData since you can interpret it from models (e.g. loadingData = models === null)
You useEffect will only occur once, when the functional component is loaded. Tracking inside then does nothing. You can remove both the tracker and setLoadingData.
In the case of an error, you can set the models as undefined (!== null so you will know the loading has ended)
I could be wrong but I don’t think you need Tracker.autorun() function here.
Btw your implementation has potential memory leaking issue. You should check if the component is mounted before updating its state. => use AbortController() if possible (thanks @superfail for your correction)