Skip to content

Instantly share code, notes, and snippets.

@rickyalmeidadev
Last active November 22, 2021 02:18
Show Gist options
  • Save rickyalmeidadev/348a2077741c97f2e862c42dfe07fbf1 to your computer and use it in GitHub Desktop.
Save rickyalmeidadev/348a2077741c97f2e862c42dfe07fbf1 to your computer and use it in GitHub Desktop.
useFocus and useRefs hooks for React and React Native forms
import {useCallback} from 'react'
const useFocus = refs => {
const focus = useCallback(
name => {
refs?.[name]?.current?.focus?.()
},
[refs]
)
return focus
}
import {createRef, useRef} from 'react'
const useRefs = keys =>
useRef(keys.reduce((refs, key) => ({...refs, [key]: createRef()}), {})).current
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment