0.5.0 • Published 1 year ago

@lumitech/mobile-hooks v0.5.0

Weekly downloads
-
License
-
Repository
github
Last release
1 year ago

React Native Hooks

React Native APIs turned into React Hooks allowing you to access asynchronous APIs directly in your functional components.

Note: You must use React Native >= 0.59.0

Installation

npm install @lumitech/mobile-hooks

API

useAppStateEvent

import { useAppStateEvent } from '@lumitech/mobile-hooks'

const [appState, setAppState] = useState('active');

useAppStateEvent({
  onChange: () => console.log(appState),
  onForeground: () => setAppState('active'),
  onBackground: () => setAppState('background'),
})

useBackHandler

import { useBackHandler } from '@lumitech/mobile-hooks'

useBackHandler(() => {
  if (shouldBeHandledHere) {
    // handle it
    return true
  }
  // let the default thing happen
  return false
})

useDeviceOrientation

import { useDeviceOrientation } from '@lumitech/mobile-hooks'

const orientation = useDeviceOrientation()

console.log('is orientation portrait: ', orientation.portrait)
console.log('is orientation landscape: ', orientation.landscape)

useDimensions

import { useScreenDimensions, useWindowDimensions } from '@lumitech/mobile-hooks'

const { width, height } = useScreenDimensions()
// or
const { width, height } = useWindowDimensions()

useInterval

import { useInterval } from '@lumitech/mobile-hooks'

const [count, setCount] = useState(0)

useInterval(() => setCount(count + 1), 1000)

useLayout

import { useLayout } from '@lumitech/mobile-hooks'

const { onLayout, ...layout } = useLayout()

console.log('layout: ', layout)

<View onLayout={onLayout} style={{width: 200, height: 200, marginTop: 30}} />

useKeyboard

import { useKeyboard } from '@lumitech/mobile-hooks'

const { keyboardShown, coordinates, keyboardHeight } = useKeyboard()

console.log('keyboard: ', { keyboardShown, coordinates, keyboardHeight })

useDebounce

import { useDebounce } from '@lumitech/mobile-hooks'

const [searchTerm, setSearchTerm] = useState("");

const debouncedSearchTerm = useDebounce(searchTerm, 500);

useTimeout

import { useTimeout } from '@lumitech/mobile-hooks'

const [count, setCount] = useState(0)

useTimeout(() => setCount(count + 1), 1000)

useThrottle

import { useThrottle } from '@lumitech/mobile-hooks'

const [searchTerm, setSearchTerm] = useState("");

const throttledSearchTerm = useThrottle(searchTerm, 500);
0.5.0

1 year ago

0.4.0

2 years ago

0.3.0

2 years ago

0.3.2

2 years ago

0.3.1

2 years ago

0.2.0

2 years ago

0.1.0

2 years ago

0.0.4

2 years ago

0.0.2

2 years ago

0.0.1

2 years ago