Skip to content

Commit 980d648

Browse files
committed
Do not render Viewport in SSR context
1 parent dc0f8ab commit 980d648

File tree

4 files changed

+25
-27
lines changed

4 files changed

+25
-27
lines changed

.size-snapshot.json

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,37 @@
11
{
22
"react-local-toast.cjs.development.js": {
3-
"bundled": 19783,
4-
"minified": 9879,
5-
"gzipped": 3030
3+
"bundled": 19762,
4+
"minified": 9871,
5+
"gzipped": 3037
66
},
77
"react-local-toast.cjs.production.js": {
8-
"bundled": 19783,
9-
"minified": 9879,
10-
"gzipped": 3030
8+
"bundled": 19762,
9+
"minified": 9871,
10+
"gzipped": 3037
1111
},
1212
"react-local-toast.esm.js": {
13-
"bundled": 18568,
14-
"minified": 8887,
15-
"gzipped": 2876,
13+
"bundled": 18547,
14+
"minified": 8879,
15+
"gzipped": 2886,
1616
"treeshaked": {
1717
"rollup": {
1818
"code": 262,
1919
"import_statements": 262
2020
},
2121
"webpack": {
22-
"code": 8771
22+
"code": 8763
2323
}
2424
}
2525
},
2626
"react-local-toast.umd.development.js": {
27-
"bundled": 110107,
28-
"minified": 34491,
29-
"gzipped": 11268
27+
"bundled": 110082,
28+
"minified": 34483,
29+
"gzipped": 11267
3030
},
3131
"react-local-toast.umd.production.js": {
32-
"bundled": 59579,
33-
"minified": 21497,
34-
"gzipped": 7178
32+
"bundled": 59554,
33+
"minified": 21489,
34+
"gzipped": 7176
3535
},
3636
"internals.js": {
3737
"bundled": 10559,

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-local-toast",
3-
"version": "1.1.3",
3+
"version": "1.1.4",
44
"description": "Local toast helps you to provide feedback related to particular components on page",
55
"main": "dist/index.js",
66
"module": "dist/react-local-toast.esm.js",
@@ -97,4 +97,4 @@
9797
"shx": "0.3.3",
9898
"typescript": "4.3.5"
9999
}
100-
}
100+
}

src/provider.tsx

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,12 @@ export const createProvider = <T,>(
106106
});
107107
};
108108

109+
// Document is unavailable in Next.js SSR, so postpone actual rendering of viewport (portal)
110+
const [mounted, setMounted] = React.useState(false);
111+
React.useEffect(() => {
112+
setMounted(true);
113+
}, []);
114+
109115
return (
110116
<Context.Provider
111117
value={{
@@ -125,7 +131,7 @@ export const createProvider = <T,>(
125131
}}
126132
>
127133
{children}
128-
<Viewport portalInto={portalInto} />
134+
{mounted && <Viewport portalInto={portalInto} />}
129135
</Context.Provider>
130136
);
131137
};

src/viewport.tsx

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -128,12 +128,6 @@ export const createViewport = <T,>(context: Context<LocalToastContextType<T>>) =
128128
);
129129
};
130130

131-
// Document is unavailable in Next.js SSR, so postpone actual rendering of portal
132-
const [mounted, setMounted] = React.useState(false);
133-
React.useEffect(() => {
134-
setMounted(true);
135-
}, []);
136-
137131
// We don't really need window size, but we need to re-render once window size changes
138132
const [width, height] = useWindowSize();
139133

@@ -216,8 +210,6 @@ export const createViewport = <T,>(context: Context<LocalToastContextType<T>>) =
216210
}
217211
});
218212

219-
if (!mounted) return null;
220-
221213
return ReactDOM.createPortal(
222214
<>
223215
<TransitionGroup component={null}>{toasts.map(renderToast)}</TransitionGroup>

0 commit comments

Comments
 (0)