useStream
React hook for repeating async operations.
Last updated
Was this helpful?
React hook for repeating async operations.
Last updated
Was this helpful?
Source code is hosted on GitHub
yarn add @corets/use-stream
This library is built on top of these packages:
Takes a function or an instance of Async
and turns it into a repeating stream of data:
import React from "react"
import { createAsync } from "@corets/async"
import { useStream } from "@corets/use-stream"
const sharedQuery = createAsync(async () => "some global data")
const Example = () => {
const stream = useStream(1000, async () => `Current date is ${new Date().toISOString()}`)
const globalStream = useStream(5000, sharedQuery, ["some", "dependencies"])
const handleReload = () => stream.run()
const handleCancel = () => stream.cancel()
if (stream.isRunning() && ! stream.getResult()) {
return <h1>Loading for the first time</h1>
}
if (stream.isErrored()) {
return <h1>There was an error :(</h1>
}
return (
<div>
Result: {stream.getResult()}
{stream.isRunning() && "Refreshing ..."}
<button onClick={handleReload}>Reload</button>
<button onClick={handleCancel}>Cancel</button>
</div>
)
}