1- # useSSE
1+ # useSSE 3.x.x-beta
2+
3+ > [ !CAUTION]
4+ > 3.x.x is still in beta
5+
6+ > [ !NOTE]
7+ > You are viewing a v3.x.x version of hook which is designed to be compatible with React 18. This version of hook is still in beta.
8+ > If you are using React <18 check latest stable [ 2.x.x version of useSSE] ( https://github.com/kmoskwiak/useSSE/tree/v2.0.1 )
29
310![ useSSE] ( https://repository-images.githubusercontent.com/262809605/78398700-a279-11ea-9ba2-4c15b6a1ec9a )
411[ ![ npm version] ( https://badgen.net/npm/v/use-sse )] ( https://www.npmjs.com/package/use-sse )
5- ![ Node.js CI] ( https://github.com/kmoskwiak/useSSE/workflows/Node.js%20CI /badge.svg?branch=master )
12+ ![ Node.js CI] ( https://github.com/kmoskwiak/useSSE/workflows/CI /badge.svg?branch=master )
613
714` useSSE ` is abbreviation for use server-side effect. It is a custom React hook to perform asynchronous effects both on client and serve side.
815
@@ -15,58 +22,77 @@ npm i use-sse
1522Use ` useSSE ` to fetch data in component:
1623
1724``` jsx
18- import React from " react" ;
1925import { useSSE } from " use-sse" ;
2026
21- const MyComponent = () => {
27+ /**
28+ * Create a custom component with effect
29+ **/
30+ const TitleComponent = () => {
2231 const [data , error ] = useSSE (() => {
2332 return fetch (" https://myapi.example.com" ).then ((res ) => res .json ());
2433 }, []);
2534
26- return < div > {data .title }< / div > ;
35+ return < h1 > {data .title }< / h1 > ;
2736};
28- ```
2937
30- All effects will be resolved on server side during rendering.
38+ /**
39+ * To take full advantage of a Suspense boundaries wrap each component in UniversalDataProvider
40+ * You can also use ServerDataProvider or BrowserDataProvider
41+ **/
42+ export const Title = () => {
43+ return (
44+ < UniversalDataProvider>
45+ < TitleComponent / >
46+ < / UniversalDataProvider>
47+ )
48+ }
49+ ```
3150
32- This is a part of server side render phase. Se an example for the whole code.
51+ Load component using Suspense API:
3352
34- ``` js
35- const { ServerDataContext , resolveData } = createServerContext ();
36-
37- // We need to render app twice.
38- // First - render App to reqister all effects
39- renderToString (
40- < ServerDataContext>
41- < App / >
42- < / ServerDataContext>
53+ ``` jsx
54+ import * as React from ' react' ;
55+ import Title from ' ./Title' ;
56+
57+ export const App = () => (
58+ < div>
59+ < React .Suspense fallback= {' Loading...' }>
60+ < Title/ >
61+ < / React .Suspense >
62+ < / div>
4363);
64+ ```
4465
45- // Wait for all effects to finish
46- const data = await resolveData ();
66+ All effects will be resolved on server side during rendering.
4767
48- // Inject into html initial data
49- res .write (data .toHtml ());
68+ This is a part of server side render phase. See an example for the whole code.
5069
51- // Render App for the second time
52- // This time data form effects will be avaliable in components
53- const htmlStream = renderToNodeStream (
54- < ServerDataContext>
55- < App / >
56- < / ServerDataContext>
57- );
70+ ``` jsx
71+ const stream = renderToPipeableStream (
72+ < App / > ,
73+ {
74+ onShellReady () {
75+ res .statusCode = didError ? 500 : 200 ;
76+ res .setHeader (' Content-type' , ' text/html' );
77+ stream .pipe (res);
78+ },
79+ onShellError () {
80+ res .statusCode = 500 ;
81+ res .send (' <h1>An error occurred</h1>' );
82+ },
83+ onError (err ) {
84+ didError = true ;
85+ console .error (err);
86+ },
87+ },
88+ );
5889```
5990
6091On client side of application use ` BroswerDataContext ` :
6192
62- ``` js
63- // This will create context will all data fetched during server side rendering
64- const BroswerDataContext = createBroswerContext ();
65-
93+ ``` jsx
6694hydrate (
67- < BroswerDataContext>
68- < App / >
69- < / BroswerDataContext> ,
95+ < App / > ,
7096 document .getElementById (" app" )
7197);
7298```
@@ -93,76 +119,7 @@ Returns an array with two elements `[data, error]`.
93119- ` data ` - resolved response from effect
94120- ` error ` - an error if effect rejected or if timeout happend.
95121
96- ---
97-
98- ### createServerContext()
99-
100- Creates server side context.
101-
102- ``` js
103- const { ServerDataContext , resolveData } = createServerContext ();
104- ```
105-
106- #### Returns
107-
108- ` ServerDataContext ` - React context provider component.
109-
110- ``` html
111- <ServerDataContext >
112- <App />
113- </ServerDataContext >
114- ```
115122
116- ` resolveData ` - function to resolve all effects.
117-
118- ``` js
119- const data = await resolveData (timeout);
120- ```
121-
122- | param | type | required | default value | description |
123- | --------- | -------- | -------- | ------------- | ----------------------------------------------- |
124- | ` timeout ` | ` number ` | false | ` undefined ` | max number of ms to wait for effects to resolve |
125-
126- ` data ` is an object containing value of context.
127-
128- Calling ` data.toHtml(variableName) ` will return a html script tak with stringified data:
129-
130- | param | type | required | default value | description |
131- | -------------- | -------- | -------- | -------------------- | ----------------------- |
132- | ` variableName ` | ` string ` | false | \_ initialDataContext | name of global variable |
133-
134- ``` js
135- data .toHtml ();
136- // "<script>window._initialDataContext = { context data };</script>"
137- ```
138-
139- Both should be used in server side render function.
140-
141- ---
142-
143- ### createBroswerContext()
144-
145- Creates client side context.
146-
147- ``` js
148- createBroswerContext (variableName);
149- ```
150-
151- #### params
152-
153- | param | type | required | default value | description |
154- | -------------- | -------- | -------- | -------------------- | ----------------------- |
155- | ` variableName ` | ` string ` | false | \_ initialDataContext | name of global variable |
156-
157- #### returns
158-
159- ` BroswerDataContext ` - React context provider for client side application
160-
161- ``` html
162- <BroswerDataContext >
163- <App />
164- </BroswerDataContext >
165- ```
166123
167124## Examples
168125
0 commit comments