Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/rescriptbr/reason-async-hook

A simple hook for sane promise handling in ReasonReact
https://github.com/rescriptbr/reason-async-hook

Last synced: 3 months ago
JSON representation

A simple hook for sane promise handling in ReasonReact

Awesome Lists containing this project

README

        







Installation
Usage
How does it work





```reason
[@react.component]
let make = () => {
let ({state: createUserState}, createUser) = AsyncHook.use((~cb, ~name, ~age) => cb(() => {
fetch("/user", ~params={
name,
age,
})
}))

let onSubmit= () => {
createUser(~name=form.values.name, ~age=form.values.age)
// Handle the result after the call
|> Promise.tapError(error => {
Js.log(error)
})
|> Promise.getOk(result => {
Js.log("Success!")
})
}

// or use the declarative mode


{
switch(createUserState) {
| Idle => onSubmit()}>"Create user"->React.string
| Loading =>

{"Loading..."->React.string}


| Error(error) =>


"User created with id #"->React.string
{data.user.id->React.string}


| Data(data) =>


"User created with id #"->React.string
{data.user.id->React.string}


}
}

}
```

# Instalação

```
yarn add reason-async-hook reason-promise
```

`bsconfig.json`
```json
"bs-dependencies": [
"reason-async-hook",
"reason-promise"
]
```

Promises são apenas melhores com reason-promise!

# Como isso funciona?

A lib apenas usa o currying por padrão e a inferência de tipos mágica do ReasonML.
`reason-promise` roda em cima das promises JavaScript mas ele adiciona mais funcionalidades a elas, e uma delas é o suporte ao tipo `result`. Permitindo controle fino sobre o fluxo da chamada e permitindo _railway programming_.