Remote API React hook for rescript
npm add @softwarefactory-project/res-remoteapi
When using pnpm
, you also need to add the dependencies to your package:
pnpm add bs-fetch remotedata-re @rescript/react react@^16.14.0
Add to your bsconfig.json
:
"bs-dependencies": [ + "@softwarefactory-project/res-remoteapi" ]
// Your application: module Main = (CLIENT: RemoteAPI.HTTPClient) => { module Hook = RemoteAPI.Hook(CLIENT) @react.component let make = () => { let state = Hook.useAutoGet("/api", api_decoder) switch (state) { | RemoteData.NotAsked | RemoteData.Loading(None) => <p> {"Loading..." |> str} </p> | RemoteData.Loading(Some(info)) | RemoteData.Success(info) => <p> {("Loaded: " ++ info.version) -> React.string} </p> | RemoteData.Failure(title) => <p> {("Failure: " ++ title) -> React.string} </p> }; } }
Contributions are most welcome.
Get started by running:
git clone https://github.com/softwarefactory-project/res-remoteapi cd re-ansi pnpm install pnpm start
Then build and run tests with pnpm test
.
Make sure to read about [React][reason-react] and ReScript too.