Skip to content

A Custom Set of Helper Hooks written in Typescript to Make Your Life Easier.

License

Notifications You must be signed in to change notification settings

fcodelabs/react-helper-hooks

Repository files navigation

@fcodelabs/react-helper-hooks

A Custom Set of Helper Hooks written in Typescript to Make Your Life Easier.

Demo

See working Demo Here

Installation

Install with npm

  npm install @fcodelabs/react-helper-hooks

Install with yarn

  yarn add @fcodelabs/react-helper-hooks

List of Hooks

  • useWindowSize - Get current window size and device type (Desktop or Mobile) based on your breakpoint
  • useLocalStorage - Sync your state with browser localStorage. Works same as the useState hook. Automatically sync with localStorage value changes.

useWindowSize

This hook is useful when you want to get the window size as height and width.

It takes one argument

  • breakpoint: number

Breakpoint for mobile in pixels

const { windowSize, isDesktop } = useWindowSize(1024);

windowSize

const { windowSize } = useWindowSize(1024);

// {width: 1368, height: 1024}

isDesktop

const { isDesktop } = useWindowSize(1024);

# If window size less than 1024
// false

# If window size greater than 1024
// true

useLocalStorage

This hook is useful when you want to use localStorage as your state. It works same almost same as useState hook, which means it will sync the localstorage value changes automatically.

It takes two arguments

  • key: string

LocalStorage key (This will be the key that your state will be saved)

  • initialState: any

Initial state value. It could be in any of data type such as string, number, boolean, object, array or custom interfaces.

## Javascript

const [ count, setCount ] = useLocalStorage('count', 0);
## Typescript

const [ count, setCount ] = useLocalStorage<number>('count', 0);

Support

If you find any bugs please report an issue, so it can be fixed. If you have any suggestions that would improve the hooks, please let us know. Feel free to contribute in any way!

We hope this package can be useful for you in someway.