Bitcoin Price
Live price fetched from CryptoCompare API with auto-refresh every 60 seconds.
Loading...
How it works
This demo uses p.Get[T]() to fetch JSON, p.SetInterval() for auto-refresh, and lifecycle hooks for setup/teardown.
type BitcoinDemo struct {
Price *p.Store[string]
Loading *p.Store[bool]
Error *p.Store[string]
stopRefresh func()
}
func (b *BitcoinDemo) OnMount() {
if p.IsBuildTime { return }
b.FetchPrice()
b.stopRefresh = p.SetInterval(60000, func() {
b.FetchPrice()
})
}
func (b *BitcoinDemo) OnDestroy() {
if b.stopRefresh != nil {
b.stopRefresh()
}
}
func (b *BitcoinDemo) FetchPrice() {
b.Loading.Set(true)
go func() {
resp, err := p.Get[PriceResponse](url)
if err != nil {
b.Error.Set(err.Error())
b.Loading.Set(false)
return
}
b.Price.Set(formatPrice(resp.RAW.PRICE))
b.Loading.Set(false)
}()
}