|
| 1 | +# react-fetch-streams |
| 2 | + |
| 3 | +> A react hook for using the [Streams API](https://developer.mozilla.org/en-US/docs/Web/API/Streams_API) with the [Fetch API](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API) to stream data from a server. |
| 4 | +
|
| 5 | +[](https://www.npmjs.com/package/react-fetch-streams) |
| 6 | +[](https://github.com/rolandjitsu/react-fetch-streams/actions?query=workflow%3ATest) |
| 7 | +[](https://coveralls.io/github/rolandjitsu/react-fetch-streams?branch=master) |
| 8 | + |
| 9 | +# Table of Contents |
| 10 | + |
| 11 | +* [Installation](#installation) |
| 12 | +* [Usage](#usage) |
| 13 | +* [Browser Support](#browser-support) |
| 14 | +* [Contribute](#contribute) |
| 15 | + |
| 16 | +### Installation |
| 17 | +---------------- |
| 18 | +You can install this package from [NPM](https://www.npmjs.com): |
| 19 | +```bash |
| 20 | +npm add react-fetch-streams |
| 21 | +``` |
| 22 | + |
| 23 | +Or with [Yarn](https://yarnpkg.com/en): |
| 24 | +```bash |
| 25 | +yarn add react-fetch-streams |
| 26 | +``` |
| 27 | + |
| 28 | +#### CDN |
| 29 | +For CDN, you can use [unpkg](https://unpkg.com): |
| 30 | + |
| 31 | +[https://unpkg.com/react-fetch-streams/dist/index.min.js](https://unpkg.com/react-fetch-streams/dist/index.min.js) |
| 32 | + |
| 33 | +The global namespace for react-fetch-streams is `reactFetchStreams`: |
| 34 | +```html |
| 35 | +<script type="text/javascript" src="https://unpkg.com/react-fetch-streams/dist/index.min.js"></script> |
| 36 | + |
| 37 | +<script type="text/javascript"> |
| 38 | + const {useStream} = reactFetchStreams; |
| 39 | + ... |
| 40 | +</script> |
| 41 | + |
| 42 | +``` |
| 43 | + |
| 44 | +### Usage |
| 45 | +--------- |
| 46 | +Stream some data from some server: |
| 47 | +```jsx |
| 48 | +import React, {useCallback, useState} from 'react'; |
| 49 | +import {useStream} from 'react-fetch-streams'; |
| 50 | + |
| 51 | +const MyComponent = props => { |
| 52 | + const [data, setData] = useState({}); |
| 53 | + const onNext = useCallback(async res => { |
| 54 | + const data = await res.json(); |
| 55 | + setData(data); |
| 56 | + }, [setData]); |
| 57 | + useStream('http://myserver.io/stream', {onNext}); |
| 58 | + |
| 59 | + return ( |
| 60 | + <React.Fragment> |
| 61 | + {data.myProp} |
| 62 | + </React.Fragment> |
| 63 | + ); |
| 64 | +}; |
| 65 | +``` |
| 66 | + |
| 67 | +You can also pass the fetch request init props using `fetchParams`: |
| 68 | +```jsx |
| 69 | +import React, {useCallback, useState} from 'react'; |
| 70 | +import {useStream} from 'react-fetch-streams'; |
| 71 | + |
| 72 | +const fetchParams = {mode: 'cors'} |
| 73 | + |
| 74 | +const MyComponent = props => { |
| 75 | + const [data, setData] = useState({}); |
| 76 | + const onNext = useCallback(async res => { |
| 77 | + const data = await res.json(); |
| 78 | + setData(data); |
| 79 | + }, [setData]); |
| 80 | + useStream('http://myserver.io/stream', {onNext, fetchParams}); |
| 81 | + |
| 82 | + return ( |
| 83 | + <React.Fragment> |
| 84 | + {data.myProp} |
| 85 | + </React.Fragment> |
| 86 | + ); |
| 87 | +}; |
| 88 | +``` |
| 89 | + |
| 90 | +For more examples, please check the [tests](./src/stream.test.js). |
| 91 | + |
| 92 | +### Browser Support |
| 93 | +------------------- |
| 94 | +You can expect this hook to work wherever the following APIs are supported: |
| 95 | +* [File API](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API#Browser_compatibility) |
| 96 | +* [Streams API](https://developer.mozilla.org/en-US/docs/Web/API/Streams_API#Browser_compatibility) |
| 97 | +* [AbortController](https://developer.mozilla.org/en-US/docs/Web/API/AbortController#Browser_compatibility) |
| 98 | +* [AbortSignal](https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal#Browser_compatibility) |
| 99 | + |
| 100 | +### Contribute |
| 101 | +-------------- |
| 102 | +If you wish to contribute, please use the following guidelines: |
| 103 | +* Use [Conventional Commits](https://conventionalcommits.org) |
0 commit comments