2020.1.9 • Published 4 years ago

@4react/hooks v2020.1.9

Weekly downloads
-
License
ISC
Repository
github
Last release
4 years ago

@4react / hooks

Common utility hooks for React Applications

Usage

Add dependency.

npm i @4react/hooks

Import the strictly necessary

import useTimeout from '@4react/useTimeout'

List of hooks

Layout

NameDescription
useDocumentSizeTriggers a callback after every resize of the document.
useElementSizeGeneric version of the useDocumentSize. Triggers the callback after every resize of an element.
useScreenOrientationTriggers the callback after every change of the screen orientation.
useWindowScrollTriggers the callback after every scroll action of the window.

State

NameDescription
useArrayManages an array state and provides helpful methods to updates it

Time

NameDescription
useIntervalIncrement a counter every ms milliseconds.
useTimeoutTurns a flag from false to true after ms milliseconds.

APIs

useArray

const { values, push, pop } = useArray()

...

<button onClick={() => push(newElement)}>PUSH</button>
<button onClick={() => pop()}>POP</button>
{values.map(el => <li>{el}</li>)}
ParamTypeDefaultDescription
initialValueany[][]Initial values for the array.
Return (object)TypeDescription
valuesany[]The elements in the array.
setValuesFunctionTake a new array of element to replace the stored one.
clearFunctionEmpty the array.
pushFunctionAdds new elements to the end of an array, and returns the new length.
popFunctionRemoves the last element of an array, and returns that element.
shiftFunctionRemoves the first element of an array, and returns that element.
unshiftFunctionAdds new elements to the beginning of an array, and returns the new length.

useDocumentSize

useDocumentSize((width, height) => {
  // do something
})
ParamTypeDefaultDescription
callbackFunction-A function called each time the document is resized. It receives new values of width and height.

useElementSize

const myElementRef = useElementSize((width, height) => {
  // do something
})

...

<MyElement ref={myElementRef} />
ParamTypeDefaultDescription
callbackFunction-A function called each time the element is resized. It receives new values of width and height.
ReturnTypeDescription
refReact.RefObjectThe reference object to pass to the element for which we want to listen for resize.

useInterval

const [counter] = useInterval(3000)

useEffect(() => {
    // do something
}, [counter])
ParamTypeDefaultDescription
msnumber-Milliseconds of the interval duration.
maxnumber-Maximum number of increment before the interval is cleared automatically.
ReturnTypeDescription
counternumberThe number of interval passed since the last reset.
clearFunctionClear the interval.
resetFunctionRestart the interval and reset the counter.

useScreenOrientation

useScreenOrientation((orientation) => {
  // do something
})
ParamTypeDefaultDescription
callbackFunction-A function called each time the screen orientation change. It receives new orientation.

useTimeout

const [elapsed] = useTimeout(3000)

useEffect(() => {
    // do something
}, [elapsed])
ParamTypeDefaultDescription
msnumber-Milliseconds of the timout duration.
ReturnTypeDescription
elapsedbooleanTrue if timeout is elapsed.
clearFunctionCancel the timeout.
resetFunctionStart a new timeout.

useWindowScroll

useWindowScroll((scrollX, scrollY) => {
  // do something
})
ParamTypeDefaultDescription
callbackFunction-A function called after every scroll action of the window. It receives new values of scrollX and scrollY.
2020.1.9

4 years ago

2020.1.8

4 years ago

2020.1.7

4 years ago

2020.1.6

4 years ago

2020.1.5

4 years ago

2020.1.4

4 years ago

2020.1.3

4 years ago

2020.1.2

4 years ago

2020.1.1

4 years ago

2020.1.0

4 years ago