WebApi is a lightweight, callback-based utility in OneJS that can fetch remote text and image resources. It uses UnityWebRequest
and Coroutines
. You can use it as-is or check out the source as a reference for making your fetch utility.
(Also to note, if you switch backend to NodeJS
, you can install and use node-fetch
directly.)
webapi.tsx
import { WebApi } from "OneJS"
import { render, h } from "preact"
import { useEffect, useRef } from "preact/hooks"
const webapi = new WebApi()
const App = () => {
const ref = useRef<Element>()
useEffect(() => {
webapi.getText("https://catfact.ninja/fact", (text) => {
console.log(text)
})
webapi.getImage("https://cdn2.thecatapi.com/images/xnzzM6MBI.jpg", (image) => {
ref.current!.style.backgroundImage = image
})
}, [])
return <div class="h-full w-full p-20 flex-row justify-between items-center">
<img class="w-60 h-60" src="https://cdn2.thecatapi.com/images/acv.jpg" />
<div ref={ref} class="w-60 h-60 bg-crop"></div>
</div>
}
render(<App />, document.body)