How to set focus on an input field after rendering in React

set focus to input react
Table of Contents

In modern React applications, managing focus on input elements is crucial for enhancing user experience. This practical guide will explore how to set focus on an input field after rendering in functional components using React 18+. We’ll delve into using the autoFocus attribute, the useRef hook, and the useEffect hook to achieve this.

Why Focus Management Matters in React

Properly managing focus in your React components ensures that users can navigate your application efficiently. Setting focus on the correct input field can guide the user to the next action, improving accessibility and usability.

Using the autoFocus Attribute

One of the simplest ways to set focus on an input field is by using the autoFocus attribute. When applied, the browser automatically focuses on that particular element during the initial render.

import React from 'react'

export default function App() {
  return (
    <form>
      <input type="text" autoFocus />
    </form>
  )
}

In this example, the input element with type="text" will be focused immediately after the component mounts.

Setting Focus with the useRef Hook

For more control over focus, especially under certain conditions, the useRef hook is essential. It allows you to create a reference to a DOM node, which you can then use to set focus programmatically.

Step 1: Create a Reference with useRef

import React, { useRef } from 'react'

export default function App() {
  const inputRef = useRef(null)

  return (
    <form>
      <input type="text" ref={inputRef} />
    </form>
  )
}

Here, we create a reference called inputRef and attach it to our input element using the ref attribute.

Step 2: Focus the Input After Rendering with useEffect

To focus the input field after the initial render, we use the useEffect hook.

import React, { useRef, useEffect } from 'react'

export default function App() {
  const inputRef = useRef(null)

  useEffect(() => {
    inputRef.current.focus()
  }, [])

  return (
    <form>
      <input type="text" ref={inputRef} />
    </form>
  )
}

The empty dependency array [] ensures the useEffect hook runs only once after the initial render. The focus method is called on the DOM element referenced by inputRef, setting focus on the input field.

Creating a Custom useFocus Hook

To simplify reusing focus logic across multiple components, you can create a custom useFocus hook. This hook will encapsulate the focus behavior, making your code cleaner and more maintainable.

JavaScript Version

import { useRef, useEffect } from 'react'

export default function useFocus() {
  const inputRef = useRef(null)

  useEffect(() => {
    inputRef.current.focus()
  }, [])

  return inputRef
}

Usage:

import React from 'react'
import useFocus from './useFocus'

export default function App() {
  const inputRef = useFocus()

  return (
    <form>
      <input type="text" ref={inputRef} />
    </form>
  )
}

TypeScript Version

import { useRef, useEffect, RefObject } from 'react'

export default function useFocus<T extends HTMLElement>(): RefObject<T> {
  const inputRef = useRef<T>(null)

  useEffect(() => {
    inputRef.current?.focus()
  }, [])

  return inputRef
}

Usage:

import React from 'react'
import useFocus from './useFocus'

export default function App() {
  const inputRef = useFocus<HTMLInputElement>()

  return (
    <form>
      <input type="text" ref={inputRef} />
    </form>
  )
}

With this custom useFocus hook, you can easily add focus behavior to any input field in your React components.

Handling Multiple Input Fields

When dealing with multiple input fields, you might want to focus on the next input based on user actions or events.

import React, { useRef } from 'react'

export default function App() {
  const firstInputRef = useRef(null)
  const secondInputRef = useRef(null)

  const handleKeyPress = (event) => {
    if (event.key === 'Enter') {
      secondInputRef.current.focus()
    }
  }

  return (
    <form>
      <input
        type="text"
        ref={firstInputRef}
        onKeyPress={handleKeyPress}
      />
      <input type="text" ref={secondInputRef} />
    </form>
  )
}

In this example, pressing the Enter key while focused on the first input moves focus to the next input.

Focusing Under Certain Conditions

Sometimes, you need to set focus on an input field only under certain conditions, such as after fetching data or based on a user interaction.

import React, { useRef, useEffect, useState } from 'react'

export default function App() {
  const inputRef = useRef(null)
  const [dataLoaded, setDataLoaded] = useState(false)

  useEffect(() => {
    // Simulate data fetching
    setTimeout(() => {
      setDataLoaded(true)
    }, 1000)
  }, [])

  useEffect(() => {
    if (dataLoaded) {
      inputRef.current.focus()
    }
  }, [dataLoaded])

  return (
    <form>
      <input type="text" ref={inputRef} />
    </form>
  )
}

Here, the input field receives focus only after the data has loaded.

Using autoFocus Prop in Custom Components

If you’re creating custom components that wrap input elements, you can pass an autoFocus prop to control focus.

import React, { forwardRef, useEffect } from 'react'

const CustomInput = forwardRef((props, ref) => {
  return <input type="text" ref={ref} {...props} />
})

export default function App() {
  const inputRef = useRef(null)

  useEffect(() => {
    inputRef.current.focus()
  }, [])

  return (
    <form>
      <CustomInput ref={inputRef} />
    </form>
  )
}

By forwarding the ref, you maintain access to the underlying DOM element.

Key Takeaways

  • Use the autoFocus attribute for simple cases where you need to focus on input during the initial page load.
  • Utilize the useRef hook to create a reference to the input element.
  • Apply the useEffect hook to set focus after the component mounts or when data changes.
  • Manage focus under certain conditions by adjusting the dependency array in the useEffect hook.
  • Remember to use const and single quotes in your code examples for consistency.

Conclusion

Managing focus in React applications is essential for creating accessible and user-friendly interfaces. By leveraging the useRef and useEffect hooks, and by creating custom hooks like useFocus, you can programmatically set focus on input fields after rendering. This ensures that your functional components behave as expected, providing a seamless experience for the user.

Subscribe to our newsletter
Get early information about new products, product updates and blog posts.
How to convert a string to boolean in JavaScript
How to convert a string to boolean in JavaScript

How to show or hide elements in React? A Step-by-Step Guide.
How to show or hide elements in React? A Step-by-Step Guide.

How to set focus on an input field after rendering in React
How to set focus on an input field after rendering in React

Passing props to child components in React function components
Passing props to child components in React function components

How to loop inside React JSX
How to loop inside React JSX

How to validate an email address in JavaScript
How to validate an email address in JavaScript