A collection of React Hooks.
The npm package @wojtekmaj/react-hooks offers a valuable collection of React Hooks designed to simplify and enhance the development of React applications. This library, crafted by developer Wojciech Maj, provides a set of custom hooks that cover a variety of common functionality needs in modern web development. By integrating these hooks into your projects, developers can streamline their codebase, improve readability, and maintain a cleaner architecture. Each hook is meticulously crafted to perform specific tasks, making them an essential toolset for any React developer seeking to optimize their application with efficient, reusable code components.
To begin using @wojtekmaj/react-hooks in your project, the installation process is straightforward. Developers can easily integrate this package into their applications by running the command npm install @wojtekmaj/react-hooks. This command pulls the latest version of the hooks library directly from npm, ensuring you have access to the most recent updates and improvements. Once installed, developers can import and use the various hooks provided by the package, such as usePrevious, useInterval, and useEvent, to enhance their application's functionality without the overhead of additional dependencies.
The @wojtekmaj/react-hooks library not only offers a robust set of tools but also boasts impressive download statistics, highlighting its popularity and reliability within the developer community. With regular updates and a strong focus on performance and security, this package is an ideal choice for developers looking to leverage advanced React features. Furthermore, the availability of source code and comprehensive documentation allows for easy customization and extension of the hooks, providing an adaptable solution tailored to the unique needs of each project. By choosing @wojtekmaj/react-hooks, developers are equipped with a powerful toolkit that promotes code reuse and best practices in React development.
Core dependencies of this npm package and its dev dependencies.
@testing-library/react, @types/react, eslint, eslint-config-wojtekmaj, happy-dom, husky, lint-staged, prettier, react, react-dom, rimraf, typescript, vitest
A README file for the @wojtekmaj/react-hooks code repository. View Code
A collection of React Hooks.
npm install @wojtekmaj/react-hooks or yarn add @wojtekmaj/react-hooks.import { useTick } from '@wojtekmaj/react-hooks'.const tick = useTick();
All hooks from this package support SSR. Some hooks use browser-only APIs, e.g. useCurrentPosition. Such hooks, if they are supposed to return a value, will return null instead.
useCurrentPositionuseDebouncedEffectuseDebouncedStateuseDebouncedValueuseEventListeneruseIntersectionObserveruseLocalStorageuseMatchMediauseMutationObserveruseOnLineusePermissionStateusePrefersColorSchemeDarkusePrefersColorSchemeLightusePrefersReducedMotionusePrefersReducedTransparencyuseResizeObserveruseScrollLeftuseScrollLeftPercentuseScrollTopuseScrollTopPercentuseSetIntervaluseSetTimeoutuseTickuseToggleuseWindowHeightuseWindowWidthuseCurrentPositionReturns current position from Geolocation API.
import { useCurrentPosition } from '@wojtekmaj/react-hooks';
useCurrentPosition(); // { latitude: 0, longitude: 0 }
useDebouncedEffectRuns a given effect after a given delay.
import { useDebouncedEffect } from '@wojtekmaj/react-hooks';
useDebouncedEffect(
() => {
// This will run after 1 second of value not changing.
},
[value],
1000,
);
useDebouncedStateReturns a debounced state and a function to update it.
import { useDebouncedState } from '@wojtekmaj/react-hooks';
const [value, setValue] = useDebouncedState('initialState', 1000); // ['initialState', Function]
useDebouncedValueReturns a debounced value.
import { useDebouncedValue } from '@wojtekmaj/react-hooks';
const debouncedValue = useDebouncedValue(value, 1000); // This value will be updated after 1 second of value not changing.
useEventListenerAdds event listener to a given element.
import { useEventListener } from '@wojtekmaj/react-hooks';
useEventListener(element, 'click', onClick);
useIntersectionObserverObserves a given element using IntersectionObserver.
import { useIntersectionObserver } from '@wojtekmaj/react-hooks';
useIntersectionObserver(element, config, onIntersectionChange);
useLocalStorageReturns a stateful value synchronized with localStorage, and a function to update it.
import { useLocalStorage } from '@wojtekmaj/react-hooks';
const [value, setValue] = useLocalStorage('myKey', 'initialState'); // ['initialState', Function]
useMatchMediaReturns a flag which determines if the document matches the given media query string.
import { useMatchMedia } from '@wojtekmaj/react-hooks';
const isDesktop = useMatchMedia('screen and (min-width: 1024px)'); // true / false
useMutationObserverObserves a given element using MutationObserver.
import { useMutationObserver } from '@wojtekmaj/react-hooks';
useMutationObserver(element, config, onMutation);
useOnLineReturns the online status of the browser.
import { useOnLine } from '@wojtekmaj/react-hooks';
const onLine = useOnLine(); // true
usePermissionStateReturns permission state given permission name.
import { usePermissionState } from '@wojtekmaj/react-hooks';
const state = usePermissionState({ name: 'geolocation' }); // 'granted'
usePrefersColorSchemeDarkReturns a flag which determines if the document matches (prefers-color-scheme: dark) media feature.
import { usePrefersColorSchemeDark } from '@wojtekmaj/react-hooks';
const prefersColorSchemeDark = usePrefersColorSchemeDark(); // true
usePrefersColorSchemeLightReturns a flag which determines if the document matches (prefers-color-scheme: light) media feature.
import { usePrefersColorSchemeLight } from '@wojtekmaj/react-hooks';
const prefersColorSchemeLight = usePrefersColorSchemeLight(); // true
usePrefersReducedMotionReturns a flag which determines if the document matches (prefers-reduced-motion: reduce) media feature.
import { usePrefersReducedMotion } from '@wojtekmaj/react-hooks';
const prefersReducedMotion = usePrefersReducedMotion(); // true
usePrefersReducedTransparencyReturns a flag which determines if the document matches (prefers-reduced-transparency: reduce) media feature.
import { usePrefersReducedTransparency } from '@wojtekmaj/react-hooks';
const prefersReducedTransparency = usePrefersReducedTransparency(); // true
useResizeObserverObserves a given element using ResizeObserver.
import { useResizeObserver } from '@wojtekmaj/react-hooks';
useResizeObserver(element, config, onResize);
useScrollLeftReturns current scroll left position in pixels.
import { useScrollLeft } from '@wojtekmaj/react-hooks';
const scrollLeft = useScrollLeft(); // 0
useScrollLeftPercentReturns current scroll left position in percentage.
import { useScrollLeftPercent } from '@wojtekmaj/react-hooks';
const scrollLeftPercent = useScrollLeftPercent(); // 0.5
useScrollTopReturns current scroll top position in pixels.
import { useScrollTop } from '@wojtekmaj/react-hooks';
const scrollTop = useScrollTop(); // 0
useScrollTopPercentReturns current scroll top position in percentage.
import { useScrollTopPercent } from '@wojtekmaj/react-hooks';
const scrollTopPercent = useScrollTopPercent(); // 0.5
useSetIntervalRuns a given function every n milliseconds.
import { useSetInterval } from '@wojtekmaj/react-hooks';
useSetInterval(fn, 1000);
useSetTimeoutRuns a given function after n milliseconds.
import { useSetTimeout } from '@wojtekmaj/react-hooks';
useSetTimeout(fn, 1000);
useTickCounts from 0, increasing the number returned every n milliseconds.
import { useTick } from '@wojtekmaj/react-hooks';
const tick = useTick(1000); // 0 … 🕐 … 1 … 🕑 … 2 …
useToggleReturns a flag and a function to toggle it.
import { useToggle } from '@wojtekmaj/react-hooks';
const [value, toggleValue] = useToggle(); // [false, Function]
useWindowHeightReturns the interior height of the window in pixels.
import { useWindowHeight } from '@wojtekmaj/react-hooks';
const windowWidth = useWindowHeight(); // 900
useWindowWidthReturns the interior width of the window in pixels.
import { useWindowWidth } from '@wojtekmaj/react-hooks';
const windowWidth = useWindowWidth(); // 1440
The MIT License.
|
|
Wojciech Maj |