LogoLogo
  • Home
  • Services
    • Fiber
    • Accessor
    • Schema
    • Form
    • Translator
  • Components
    • <Router/>
    • <Memo />
  • Observables
    • Async
    • Value
    • Store
    • List
    • Local Storage Value
    • Local Storage Store
    • Local Storage List
  • Hooks
    • useAsync
    • useAffect
    • useStream
    • useAction
    • useDebounce
    • useThrottle
    • usePrevious
    • useIdle
    • useValue
    • useList
    • useStore
    • useForm
    • useFormBinder
    • useTranslator
    • useQuery
  • Helpers
    • Tag
    • Input Helpers
    • Promise Helpers
    • Save Helpers
    • Pagination Helpers
    • Clipboard Helpers
    • Calendar Helpers
    • Local Storage Helpers
Powered by GitBook
On this page

Was this helpful?

  1. Hooks

useStream

React hook for repeating async operations.

PrevioususeAffectNextuseAction

Last updated 3 years ago

Was this helpful?

Source code is hosted on

yarn add @corets/use-stream
npm install --save @corets/use-stream

This library is built on top of these packages:

useStream()

Takes a function or an instance of 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>
  )
}

Check out and documentation for more details.

GitHub
Async
useAsync
Async
Async
useAsync