
Security News
TypeScript is Porting Its Compiler to Go for 10x Faster Builds
TypeScript is porting its compiler to Go, delivering 10x faster builds, lower memory usage, and improved editor performance for a smoother developer experience.
react-fetch-streams
Advanced tools
A react hook for using the Streams API with the Fetch API to stream data from a server.
You can install this package from NPM:
npm add react-fetch-streams
Or with Yarn:
yarn add react-fetch-streams
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>
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.
You can expect this hook to work wherever the following APIs are supported:
Check browserslist.dev for an overview.
If you wish to contribute, please use the following guidelines:
FAQs
React hook for the Streams API
The npm package react-fetch-streams receives a total of 175 weekly downloads. As such, react-fetch-streams popularity was classified as not popular.
We found that react-fetch-streams demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
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.
Security News
TypeScript is porting its compiler to Go, delivering 10x faster builds, lower memory usage, and improved editor performance for a smoother developer experience.
Research
Security News
The Socket Research Team has discovered six new malicious npm packages linked to North Korea’s Lazarus Group, designed to steal credentials and deploy backdoors.
Security News
Socket CEO Feross Aboukhadijeh discusses the open web, open source security, and how Socket tackles software supply chain attacks on The Pair Program podcast.