Skip to content
Source
Sign inSign up
TanStack/router
Open History to see the last commit

WindowSize.tsx

19 lines660 Bytes

1/**
2 * This component accesses `window` at module scope, which would throw
3 * if this module is ever imported on the server. The compiler optimization
4 * for <ClientOnly> ensures this module is DCE'd from the server bundle
5 * entirely, preventing the error.
6 */
7​
8// This throws at module import time on the server since `window` doesn't exist
9const initialWidth = window.innerWidth
10const initialHeight = window.innerHeight
11​
12export function WindowSize() {
13 return (
14 <div data-testid="window-size">
15 <p data-testid="window-width">Window width: {initialWidth}</p>
16 <p data-testid="window-height">Window height: {initialHeight}</p>
17 </div>
18 )