@react-native-community/hooks
TypeScript icon, indicating that this package has built-in type declarations

3.0.0 • Public • Published

React Native Hooks

React Native Hooks

Version

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 with npm

npm install @react-native-community/hooks

Installation with yarn

yarn add @react-native-community/hooks

API

useAccessibilityInfo

import {useAccessibilityInfo} from '@react-native-community/hooks'

const {
  boldTextEnabled,
  screenReaderEnabled,
  reduceMotionEnabled, // requires RN60 or newer
  grayscaleEnabled, // requires RN60 or newer
  invertColorsEnabled, // requires RN60 or newer
  reduceTransparencyEnabled, // requires RN60 or newer
} = useAccessibilityInfo()

useAppState

AppState will change between one of 'active', 'background', or (iOS) 'inactive' when the app is closed or put into the background.

import {useAppState} from '@react-native-community/hooks'

const currentAppState = useAppState()

useBackHandler

import {useBackHandler} from '@react-native-community/hooks'

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

useImageDimensions

import {useImageDimensions} from '@react-native-community/hooks'

const source = require('./assets/yourImage.png')
// or
const source = {uri: 'https://your.image.URI'}

const {dimensions, loading, error} = useImageDimensions(source)

if (loading || error || !dimensions) {
  return null
}
const {width, height, aspectRatio} = dimensions

useKeyboard

import {useKeyboard} from '@react-native-community/hooks'

const keyboard = useKeyboard()

console.log('keyboard isKeyboardShow: ', keyboard.keyboardShown)
console.log('keyboard keyboardHeight: ', keyboard.keyboardHeight)

useInteractionManager

import {useInteractionManager} from '@react-native-community/hooks'

const interactionReady = useInteractionManager()

console.log('interaction ready: ', interactionReady)

useDeviceOrientation

import {useDeviceOrientation} from '@react-native-community/hooks'

const orientation = useDeviceOrientation()

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

useLayout

import { useLayout } from '@react-native-community/hooks'

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

console.log('layout: ', layout)

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

useRefresh

import { useRefresh } from '@react-native-community/hooks'

const fetch = () => {
    return new Promise((resolve) => setTimeout(resolve, 500))
}

const { isRefreshing, onRefresh } = useRefresh(fetch);

<ScrollView 
  refreshControl= {
    <RefreshControl
      refreshing={isRefreshing}
      onRefresh={onRefresh}
    />
  }
/>

Thanks

We use auto for automatic releases, an awesome tool by an awesome dude!

Contributors

Thanks goes to these wonderful people (emoji key):


Pavlos Vinieratos

💻 🎨 📖 🚇 🚧 ⚠️

Melih

💻 📖 🚇 ⚠️

Jesse Katsumata

💻 📖 🚧 ⚠️

abhishek gupta

🐛

Zeljko

💻

Linus Unnebäck

💻 🚇 📖 ⚠️

Tony Xiao

💻

Ronaldo Lima

💻

Marius Reimer

💻

Nishith Patel

💻

jozn

📖

Andrew Lisowski

🚇 📦 🔧 💻 📖

faraz ahmad

📖

Nader Dabit

🤔

Dani Akash

💻

Dylan Vann

💻

Tihomir Valkanov

📖

Pierre Skowron

💻

Gamal Shaban

💻 📖 ⚠️

Greg-Bush

📖 💻 ⚠️ 🚇

Alan Kenyon

📖 💻

Thibault Maekelbergh

⚠️ 💻

Gertjan Reynaert

⚠️ 💻

David NRB

⚠️ 💻 📖

This project follows the all-contributors specification. Contributions of any kind welcome!

Versions

Current Tags

VersionDownloads (Last 7 Days)Tag
3.0.040,782latest
3.0.0--canary.323.4060314457.01canary

Version History

VersionDownloads (Last 7 Days)Published
3.0.040,782
3.0.0--canary.323.4060314457.01
3.0.0--canary.323.4060284859.00
2.8.137,230
2.8.0842
2.7.01
2.6.01,977
2.5.1167
2.5.00
2.4.97
2.4.85
2.4.79
2.4.7-canary.89.248.00
2.4.65
2.4.6-canary.80.195.00
2.4.57
2.4.42
2.4.4-canary.78.181.00
2.4.4-canary.undefined.175.00
2.4.4-canary.76.172.00
2.4.4-canary.74.152.00
2.4.4-canary.74.149.00
2.4.4-canary.74.147.00
2.4.4-canary.74.143.00
2.4.32
2.4.3-canary.72.136.00
2.4.3-canary.71.127.00
2.4.21
2.4.2-canary.70.111.00
2.4.2-canary.69.105.00
2.3.10
2.3.1-canary.65.77.00
2.3.02
2.2.01
2.1.01
2.1.0-canary.64.64.00
2.1.0-canary.64.61.00
2.0.52
2.0.41
2.1.0-canary.63.46.00
2.0.3-canary.undefined.41.00
2.0.3-canary.29c26cb.00
2.0.21
2.0.4-canary.43ab490.00
2.0.04
1.1.01

Package Sidebar

Install

npm i @react-native-community/hooks

Weekly Downloads

81,051

Version

3.0.0

License

ISC

Unpacked Size

111 kB

Total Files

75

Last publish

Collaborators

  • pvinis