27 | {eventSourceStatus === "open" ? null :
}
28 | {messages.map((msg) => (
29 |
{msg.text}
30 | ))}
31 |
32 | );
33 | }
34 | ```
35 |
--------------------------------------------------------------------------------
/packages/use-event-source/docs/setup.md:
--------------------------------------------------------------------------------
1 | # Setup
2 |
3 | This library is shipped as es2015 modules. To use them in browsers, you'll have to transpile them using webpack or similar, which you probably already do.
4 |
5 | ## Install via NPM
6 |
7 | ```bash
8 | npm i @react-nano/use-event-source
9 | ```
10 |
--------------------------------------------------------------------------------
/packages/use-event-source/docs/usage.md:
--------------------------------------------------------------------------------
1 | # Usage
2 |
3 | ## Example
4 |
5 | In order to subscribe to an SSE endpoint, you need to call useEventSource.
6 | After you have it, you can add listeners to it. Here's a simple example:
7 |
8 | ```tsx
9 | import { useEventSource, useEventSourceListener } from "@react-nano/use-event-source";
10 |
11 | function MyComponent() {
12 | const [messages, addMessages] = useReducer(messageReducer, []);
13 |
14 | const [eventSource, eventSourceStatus] = useEventSource("api/events", true);
15 | useEventSourceListener(
16 | eventSource,
17 | ["update"],
18 | (evt) => {
19 | addMessages(JSON.parse(evt.data));
20 | },
21 | [addMessages],
22 | );
23 |
24 | return (
25 |