reason-async-hook
reason-async-hook copied to clipboard
feat: add demo
This PR will add a demo
Which fetch function should be used here
fetch("/user", ~params={
name,
age,
})
I was trying to use bs-fetch, but the API seems different and it doesn't return the expected type either. Should I use something else? Also, I can fetch from basically any API, right? For demo purposes, a placeholder API like this https://jsonplaceholder.typicode.com/users should be fine I guess.
Another thing I've noticed is that there are form values somewhere on the README snippet. Should I use https://github.com/Astrocoders/reform and get the values from there?
I think it could be just a useState to keep the demo simpler! About the endpoint it could be a simple fastify server that we can spin up together with Webpack like in here https://github.com/fakenickels/es2077-course-slides/blob/master/forms-com-reason/server.js, everything needed for it's on this file and the only dependencies are fastify and fastify-cors
@guilhermedeandrade if u are using bs-fetch, you can use reason-promise to parse the result from Js.Promise.t to Promise.t, something like that:
let requestData = url => Fetch.fetch(url)|>Promise.Js.fromBsPromise|>Promise.Js.toResult;
[@react.component]
let make = () => {
let (AsyncHook.{ state }, _) = AsyncHook.use((~cb)=> cb(() => requestData(url)));
<div>
<form>
</form>
</div>
};
@guilhermedeandrade if u are using
bs-fetch, you can usereason-promiseto parse the result from Js.Promise.t to Promise.t, something like that:let requestData = url => Fetch.fetch(url)|>Promise.Js.fromBsPromise|>Promise.Js.toResult; [@react.component] let make = () => { let (AsyncHook.{ state }, _) = AsyncHook.use((~cb)=> cb(() => requestData(url))); <div> <form> </form> </div> };
Thank you @vmarcosp! I'll keep this in mind once I start working on it again
@fakenickels @vmarcosp guys, I'd like to say that I'm sorry but I won't be able to keep working on this (at least not in the near future). I need to focus on some personal things right now. Since I'm still learning Reason, this PR is really challenging and, thus, time-consuming for me.
No problem! 👊
Hey no worries at all! I'm going to reopen your PR so we can continue and merge!