Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

react-fetch-streams

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

react-fetch-streams

React hook for the Streams API

  • 1.1.2
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
304
increased by80.95%
Maintainers
1
Weekly downloads
 
Created
Source

react-fetch-streams

A react hook for using the Streams API with the Fetch API to stream data from a server.

npm GitHub Workflow Status Coveralls Github Branch

Table of Contents

Installation


You can install this package from NPM:

npm add react-fetch-streams

Or with Yarn:

yarn add react-fetch-streams
CDN

For CDN, you can use unpkg:

https://unpkg.com/react-fetch-streams/dist/index.min.js

The global namespace for react-fetch-streams is reactFetchStreams:

<script type="text/javascript" src="https://unpkg.com/react-fetch-streams/dist/index.min.js"></script>

<script type="text/javascript">
    const {useStream} = reactFetchStreams;
    ...
</script>

Usage


Stream some data from some server:

import React, {useCallback, useState} from 'react';
import {useStream} from 'react-fetch-streams';

const MyComponent = props => {
    const [data, setData] = useState({});
    const onNext = useCallback(async res => {
        const data = await res.json();
        setData(data);
    }, [setData]);
    useStream('http://myserver.io/stream', {onNext});

    return (
        <React.Fragment>
            {data.myProp}
        </React.Fragment>
    );
};

You can also pass the fetch request init props using fetchParams:

import React, {useCallback, useState} from 'react';
import {useStream} from 'react-fetch-streams';

const fetchParams = {mode: 'cors'}

const MyComponent = props => {
    const [data, setData] = useState({});
    const onNext = useCallback(async res => {
        const data = await res.json();
        setData(data);
    }, [setData]);
    useStream('http://myserver.io/stream', {onNext, fetchParams});

    return (
        <React.Fragment>
            {data.myProp}
        </React.Fragment>
    );
};

For more examples, please check the tests.

Browser Support


You can expect this hook to work wherever the following APIs are supported:

Check browserslist.dev for an overview.

Contribute


If you wish to contribute, please use the following guidelines:

Keywords

FAQs

Package last updated on 22 Nov 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc