1.28.0 • Published 6 months ago

react-ext-hooks v1.28.0

Weekly downloads
-
License
BSD-2-Clause
Repository
github
Last release
6 months ago

react-ext-hooks

It is a library of React hooks that extends its capabilities. Both types of modules (ESM / CommonJS) are supported.

react ext hooks set

Content:

Install

Usage

Store hooks:

useLocalStorage() šŸŖ

useSessionStorage() šŸŖ

Render hook:

useReRender() šŸŖ

useRsizeObserver() šŸŖ

Other:

Examples in CodeSandbox

Install

npm:

npm i react-ext-hooks

yarn:

yarn add react-ext-hooks

Import

To import all the hooks, use:

import {
  useLocalStorage,
  useSessionStorage,
  useReRender,
} from 'react-ext-hooks';

...or you can import only the ones you need.

Usage

useLocalStorage

šŸŖšŸŖšŸŖ Description šŸ’

useLocalStorage is a hook for React that allows you to write to local storage data in the format of simple types or your objects. The hook also allows you to read data from local storage and causes the component to re-render when the data in local storage changes. In case there is no data in the local storage, the hook will write and return the default value you specify.

API 🚩

useLocalStorage(key: string, defaultValue?:T, options?:Options)

Returns value, setValue when called. The return values work similarly to their counterparts in setState() hook.

key: string Key for identifying the value to be stored.

defaultValue?: T Default value. The argument is optional, but it is better to specify it in case the value is absent in the storage. Values of type object pass serialization on writing (default JSON.stringify()) and parsing on reading (default JSON.parse()).

options?: Options Optional argument, has the following properties:

options: {
    syncData?: boolean,
    serializer?: function,
    parser?: function,
    logger?: function,
};

syncData?: boolean Allows you to synchronize value change events between different hook instances. The default value is true.

serializer?: function Allows you to specify your serializer function instead of the default. The default function is JSON.stringify.

parser?: function Allows you to specify your parser function instead of the default. The default function is JSON.parse.

logger?: function Allows you to specify your own error logging function instead of the default. The default function is console.log.

Example āœ

The most basic use of a hook involves calling the hook function with one parameter - the name of the local storage key. However, it is recommended to use it with a second parameter - the default value:

//local-storage-input.js file:
import { useLocalStorage } from 'react-ext-hooks';

export function LocalStorageInput() {
  const [value, setValue] = useLocalStorage('example1', 'Default text');
  return (
    <>
      <div>The text bellow is stored in Local Storage:</div>
      <input
        value={value}
        onChange={(e) => {
          setValue(e.target.value);
        }}
      />
    </>
  );
}

//add to usage file:
<LocalStorageInput />;

Features āœ…

āœ“ Built-in work with different data, including objects

āœ“ Support SSR

āœ“ Synchronization between all hook calls when data changes

āœ“ Written with TypeScript

useSessionStorage

šŸŖšŸŖšŸŖ Description šŸ’

useSessionStorage is a hook for React that allows you to write to session storage data in the format of simple types or your objects. The hook also allows you to read data from session storage and causes the component to re-render when the data in session storage changes. In case there is no data in the session storage, the hook will write and return the default value you specify.

API 🚩

useSessionStorage(key: string, defaultValue?:T, options?:Options)

Returns value, setValue when called. The return values work similarly to their counterparts in setState() hook.

key: string Key for identifying the value to be stored.

defaultValue?: T Default value. The argument is optional, but it is better to specify it in case the value is absent in the storage. Values of type object pass serialization on writing (default JSON.stringify()) and parsing on reading (default JSON.parse()).

options?: Options Optional argument, has the following properties:

options: {
    syncData?: boolean,
    serializer?: function,
    parser?: function,
    logger?: function,
};

syncData?: boolean Allows you to synchronize value change events between different hook instances. The default value is true.

serializer?: function Allows you to specify your serializer function instead of the default. The default function is JSON.stringify.

parser?: function Allows you to specify your parser function instead of the default. The default function is JSON.parse.

logger?: function Allows you to specify your own error logging function instead of the default. The default function is console.log.

Example āœ

The most basic use of a hook involves calling the hook function with one parameter - the name of the session storage key. However, it is recommended to use it with a second parameter - the default value:

//session-storage-input.js file:
import { useSessionStorage } from 'react-ext-hooks';

export function SessionStorageInput() {
  const [value, setValue] = useSessionStorage('example1', 'Default text');
  return (
    <>
      <div>The text bellow is stored in Session Storage:</div>
      <input
        value={value}
        onChange={(e) => {
          setValue(e.target.value);
        }}
      />
    </>
  );
}

//add to usage file:
<SessionStorageInput />;

Features āœ…

āœ“ Built-in work with different data, including objects

āœ“ Support SSR

āœ“ Synchronization between all hook calls when data changes

āœ“ Written with TypeScript

useReRender

šŸŖšŸŖšŸŖ Description šŸ’

useReRender this is a very simple hook that returns a function that, when called, re-renders the component in which the hook is declared.

API 🚩

useReRender()

Returns callReRender function when called. You need to call this function when you need to rerender callReRender().

Example āœ

//rerender-tesp-panel.js file:
import { useReRender } from 'react-ext-hooks';

export function ReRenderTestPanel() {
  const callReRender = useReRender();
  const renderCount = useRef(0);
  useEffect(function onReRender() {
    renderCount.current++;
  });
  return (
    <div>
      <div className='re_render_panel'>
        <div>Current render count: {renderCount.current}</div>
        <button onClick={() => callReRender()}>Call render</button>
      </div>
    </div>
  );
}

//add to usage file:
<ReRenderTestPanel />;

Features āœ…

āœ“ Support SSR

āœ“ Written with TypeScript

useResizeObserver

šŸŖšŸŖšŸŖ Description šŸ’

useResizeObserver this is a simple hook that allows to observe for resizes of a ref containing an HTML element.

API 🚩

To watch the entire page use: useResizeObserver(watchEntirePage: true)

or to watch an component use overload function: useResizeObserver(elementRef: React.MutableRefObject, parentLevel?:number)

Returns object {width?: number, height?: number} when called. Property width of return object contain width of element or page in pixels. Property height of return object contain height of element or page in pixels;

watchEntirePage: true Pass this argument with true value to watch the entire page.

elementRef: React.MutableRefObject Reference to an element defined using a react hook useRef.

parentLevel?:number Optional argument, is needed to watch the parent HTML element. The parent level is specified by a number passed in this argument/

Example āœ

The most basic use of a hook involves calling the hook function with one parameter - the name of the local storage key. However, it is recommended to use it with a second parameter - the default value:

//resize-counter.js file:
import { useResizeObserver } from 'react-ext-hooks';

function ResizeCounter() {
  const resizeCount = useRef(0);
  const { width, height } = useResizeObserver({
    watchEntirePage: true,
  });
  useEffect(
    function onReRender() {
      resizeCount.current++;
    },
    [width, height]
  );
  return (
    <div>
      <div>Current resize count: {resizeCount.current}</div>
      <div>Current width: {width}</div>
      <div>Current height: {height}</div>
    </div>
  );
}
//add to usage file:
<ResizeCounter />;

Features āœ…

āœ“ Polyfill is used

āœ“ Support SSR

āœ“ Written with TypeScript

Other

šŸ”„ All the examples of usage react-ext-hooks are in the sandbox.

1.22.0

6 months ago

1.25.0

6 months ago

1.26.0

6 months ago

1.24.0

6 months ago

1.27.0

6 months ago

1.28.0

6 months ago

1.20.0

11 months ago

1.19.0

11 months ago

1.18.0

11 months ago

1.17.0

11 months ago

1.16.0

11 months ago

1.15.0

11 months ago

1.14.0

11 months ago

1.13.2

11 months ago

1.13.1

11 months ago

1.13.0

11 months ago

1.12.1

11 months ago

1.12.0

11 months ago

1.11.0

11 months ago

1.10.0

11 months ago

1.9.0

11 months ago

1.8.0

11 months ago

1.7.0

11 months ago

1.6.0

11 months ago

1.5.0

12 months ago

1.4.0

12 months ago

1.3.0

12 months ago

1.1.0

12 months ago

1.0.4

12 months ago

1.0.3

12 months ago

1.0.2

12 months ago

1.0.1

12 months ago

1.0.0

12 months ago