How to Use Standard Program in Bucklescript-TEA
When you first start Bucklescript-TEA, you’ll probably use the initial beginnerProgram
1.
So what you first start of with is something like this:
let main =
App.beginnerProgram
{ model
; update
; view
}
Which works perfectly fine, but you’ll quickly outgrow it. For example if want to be able to handle Commands, then you need to switch to a standardProgram
2.
So, simply, it just looks like this:
let main =
App.standardProgram
{ init
; update
; view
; subscriptions = (fun _ -> Sub.none)
}
For subscriptions, if you aren’t using them, just do what I did there and return a function with Sub.none
The model is replaced by init
function:
type model =
{ state : string
}
let init () =
({ state = "initial string" }, Cmd.none)
It’s simply returns a 2 element tuple with the model in the first element and a Command in the second.
Your update
function also returns the a 2 element tuple same as init
except with an updated model.
type msg =
| UpdateState of string
let update model msg =
match msg with
| UpdateState new_state ->
({ model with state = new_state }, Cmd.none)
The view is the same and you also get to interact with Subscriptions.