Skip to content

How to get viewport dimensions in reflex? #2462

Answered by masenf
masenf asked this question in Q&A
Discussion options

You must be logged in to vote

How to Get Viewport in JS

The viewport dimensions can be accessed in javascript via the window.innerWidth and window.innerHeight attributes. When the viewport size changes, the browser fires the resize event on the window.

How to Get Viewport in Reflex

Using a custom react hook, we can register a reflex state EventHandler as the window.onresize event handler and get the latest viewport dimensions whenever they change.

Custom Reflex Component

Edit: updated to work with Reflex 0.6.7+

ResizeWatcher will extend rx.Fragment, which does not render anything in the DOM.

Define on_resize as an EventHandler with the required function signature so that it can be linked to an event handler in the sta…

Replies: 1 comment 3 replies

Comment options

masenf
Jan 26, 2024
Maintainer Author

You must be logged in to vote
3 replies
@matejkadlec
Comment options

@benedikt-bartscher
Comment options

@masenf
Comment options

masenf Jan 8, 2025
Maintainer Author

Answer selected by masenf
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants