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 )
2
9
3
10
![ useSSE] ( https://repository-images.githubusercontent.com/262809605/78398700-a279-11ea-9ba2-4c15b6a1ec9a )
4
11
[ ![ 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 )
6
13
7
14
` useSSE ` is abbreviation for use server-side effect. It is a custom React hook to perform asynchronous effects both on client and serve side.
8
15
@@ -15,58 +22,77 @@ npm i use-sse
15
22
Use ` useSSE ` to fetch data in component:
16
23
17
24
``` jsx
18
- import React from " react" ;
19
25
import { useSSE } from " use-sse" ;
20
26
21
- const MyComponent = () => {
27
+ /**
28
+ * Create a custom component with effect
29
+ **/
30
+ const TitleComponent = () => {
22
31
const [data , error ] = useSSE (() => {
23
32
return fetch (" https://myapi.example.com" ).then ((res ) => res .json ());
24
33
}, []);
25
34
26
- return < div > {data .title }< / div > ;
35
+ return < h1 > {data .title }< / h1 > ;
27
36
};
28
- ```
29
37
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
+ ```
31
50
32
- This is a part of server side render phase. Se an example for the whole code.
51
+ Load component using Suspense API:
33
52
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>
43
63
);
64
+ ```
44
65
45
- // Wait for all effects to finish
46
- const data = await resolveData ();
66
+ All effects will be resolved on server side during rendering.
47
67
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.
50
69
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
+ );
58
89
```
59
90
60
91
On client side of application use ` BroswerDataContext ` :
61
92
62
- ``` js
63
- // This will create context will all data fetched during server side rendering
64
- const BroswerDataContext = createBroswerContext ();
65
-
93
+ ``` jsx
66
94
hydrate (
67
- < BroswerDataContext>
68
- < App / >
69
- < / BroswerDataContext> ,
95
+ < App / > ,
70
96
document .getElementById (" app" )
71
97
);
72
98
```
@@ -93,76 +119,7 @@ Returns an array with two elements `[data, error]`.
93
119
- ` data ` - resolved response from effect
94
120
- ` error ` - an error if effect rejected or if timeout happend.
95
121
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
- ```
115
122
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
- ```
166
123
167
124
## Examples
168
125
0 commit comments