⚙️ React Window hooks
About
Handle window events and observe window size
Key Features
🔮 Simple usage
🚀 Fast and light
✨ Lifecycle Window events
💎 Lifecycle Document events
🎯 Window size
🪄 Window scroll position
💡 Window focus
🎊 SSR Support
Installation
npm install --save @better-hooks/window
or
yarn add @better-hooks/window
Examples
useWindowEvent
import React from "react";
import { useWindowEvent } from "@better-hooks/window";
const MyComponent: React.FC = () => {
useWindowEvent("scroll", () => {
});
useWindowEvent("wheel", () => {
});
useWindowEvent("resize", () => {
});
return (
)
}
useDocumentEvent
import React from "react";
import { useDocumentEvent } from "@better-hooks/window";
const MyComponent: React.FC = () => {
useDocumentEvent("visibilitychange", () => {
});
useDocumentEvent("scroll", () => {
});
return (
)
}
useWindowSize
import React from "react";
import { useWindowSize } from "@better-hooks/window";
const MyComponent: React.FC = () => {
const [width, height] = useWindowSize()
return (
)
}
useWindowScroll
import React from "react";
import { useWindowScroll } from "@better-hooks/window";
const MyComponent: React.FC = () => {
const [x, y] = useWindowScroll()
return (
)
}
useWindowFocus
import React from "react";
import { useWindowFocus } from "@better-hooks/window";
const MyComponent: React.FC = () => {
const focus = useWindowFocus()
useEffect(() => {
if(focus) {
} else {
}
}, [focus])
return (
)
}