npm i @npm.piece/utils
yarn add @npm.piece/utils
import { useDebounce } from 'libs/utils/dist/index'
const log = useDebounce((params) => console.log(params), 1000);
log("123")
import { useThrottle } from '@npm.piece/utils'
const log = useThrottle((params) => console.log(params), 1000);
log("123")
import { usePortraitDetect } from '@npm.piece/utils'
const isPortrait = usePortraitDetect()
import { useOnlineDetect } from '@npm.piece/utils'
const isOnline = useOnlineDetect()
import { useSizeDetect } from '@npm.piece/utils'
const {
clientHeight,
clientWidth,
innerHeight,
innerWidth
} = useSizeDetect()
import { useKeyPressDetect } from '@npm.piece/utils'
const isKeyFPressed = useKeyPressDetect("f")
import { useClickOutside } from '@npm.piece/utils'
const ref = useRef()
useClickOutside(ref, () => {
})
import { useOnKeyPress } from '@npm.piece/utils'
const callback = useCallback(() => {
// i use toLowerCase() in code, so it doesn't matter, if you use 'Enter' or 'enter'
console.log('npm.piece')
}, [])
useOnKeyPress(callback, 'Enter')
import { useInterval } from '@npm.piece/utils'
useInterval(() => {
}, 1000);
import { useTimeout } from '@npm.piece/utils'
useTimeout(() => {
}, 1000);
import { useFocus } from '@npm.piece/utils'
const checkViewPortRef = useRef < HTMLDivElement > (null);
const setFocus = useFocus(ref)
useEffect(() => {
setFocus()
}, [])
return <input ref={htmlElRef} />
import { useIsVisibleElement } from '@npm.piece/utils'
const checkViewPortRef = useRef < HTMLDivElement > (null);
const isInViewPort = useIntersection(checkViewPortRef);
return <div ref={checkViewPortRef} />
import { useEffectWithoutFirstCall } from '@npm.piece/utils'
useEffectWithoutFirstCall(() => {
}, []);
import { ServiceInjector } from '@npm.piece/utils'
<ServiceInjector
services={[condition.service, mobile.service]}
/>
import { ErrorBoundary } from '@npm.piece/utils'
<ErrorBoundary errorComponent={<h1>error</h1>}>
<div/>
</ErrorBoundary>
import { ArrayRender } from '@npm.piece/utils'
<ArrayRender items={items} renderItem={(item) => <itemTemplate key />} />
import { deepClone } from '@npm.piece/utils'
const newObj = deepClone({ name: 'npm.piece' })
import { mergeObjects } from '@npm.piece/utils'
const a = {
name: 'npm.piece',
location: {
city: 'City 17'
}
}
const b = {
age: 18,
location: {
flat: 'H15'
}
}
const c = {
price: 100
}
const d = mergeObjects(a, b, c)
// d will be:
const d = {
name: 'npm.piece',
age: 18,
price: 100,
location: {
city: 'City 17',
flat: 'H15'
}
}
import {
setToSessionStorage,
setToLocalStorage,
getFromSessionStorage,
getFromLocalStorage
} from '@npm.piece/utils'
setToSessionStorage('token', { age: 100 })
setToLocalStorage('token', { age: 100 }),
getFromSessionStorage('token'),
getFromLocalStorage('token')
moved to event-bus
import { IndexedDB } from '@npm.piece/utils'
useEffect(() => {
const runIndexDb = async () => {
const idb = new IndexedDB('test')
//if you need to delete database, add new version number for second argument
await idb.createObjectStore(['languages', 'students'], 1)
await idb.putValue('languages', { name: 'JavaScript' })
await idb.putBulkValue('languages', [
{ name: 'TypeScript' },
{ name: 'C#' }
])
await idb.getValue('languages', 1)
await idb.getAllValue('languages')
await idb.deleteValue('languages', 1)
}
runIndexDb()
}, [])
import { createQueryParams } from '@npm.piece/utils'
createQueryParams({ page: 1, size:10 })
import { useQueryParams } from '@npm.piece/utils'
const params = useQueryParams()