react-otp-input vs react-native-otp-entry
OTP Input Libraries for React Comparison
1 Year
react-otp-inputreact-native-otp-entry
What's OTP Input Libraries for React?

OTP (One-Time Password) input libraries are designed to facilitate the user experience when entering OTP codes for authentication purposes. These libraries provide customizable input fields that can handle multiple digits, often with features like auto-focus, auto-submit, and validation. They are essential for applications that require secure user verification through OTPs, enhancing both usability and security.

Package Weekly Downloads Trend
Github Stars Ranking
Stat Detail
Package
Downloads
Stars
Size
Issues
Publish
License
react-otp-input209,855690142 kB462 years agoMIT
react-native-otp-entry52,70536528.6 kB625 days agoMIT
Feature Comparison: react-otp-input vs react-native-otp-entry

Customization

  • react-otp-input:

    'react-otp-input' offers a range of customization options, allowing developers to adjust the appearance of the input fields, including font size, padding, and background color. It also supports custom input masks and validation rules, making it adaptable to various design requirements.

  • react-native-otp-entry:

    This library allows extensive customization of the input fields, including styling options for borders, colors, and sizes, enabling developers to match the input fields with their application's design. It also supports custom input validation and error handling, enhancing the user experience.

User Experience Features

  • react-otp-input:

    This library provides a smooth user experience with features like auto-focusing on the next input field and the ability to handle paste events, allowing users to paste their OTP directly into the input fields. It also provides visual feedback for input validation.

  • react-native-otp-entry:

    Designed for mobile, this library includes features like auto-focus on the next input field after entering a digit, which streamlines the user experience. It also supports keyboard type adjustments for numeric input, making it user-friendly for mobile users.

Integration

  • react-otp-input:

    This library is designed for easy integration into both React and React Native applications. Its simple API allows for quick setup, making it suitable for projects that require rapid development and deployment.

  • react-native-otp-entry:

    Integrates seamlessly with React Native applications, providing a native feel and performance. It is designed to work well with other React Native components and libraries, ensuring a cohesive development experience.

Validation and Error Handling

  • react-otp-input:

    Includes validation capabilities that allow developers to enforce OTP length and format. It provides visual cues for incorrect input, helping users correct mistakes before submission.

  • react-native-otp-entry:

    Offers built-in validation features that can be customized to fit specific requirements. Developers can define rules for OTP length and format, ensuring that user input is validated before submission, which enhances security.

Community and Support

  • react-otp-input:

    This library also enjoys a solid community presence, with ample documentation and examples available. Active maintenance and updates ensure that it stays compatible with the latest versions of React and React Native.

  • react-native-otp-entry:

    Being a popular choice among React Native developers, this library has a growing community and active support channels. This ensures that developers can find help and resources easily, contributing to faster problem resolution.

How to Choose: react-otp-input vs react-native-otp-entry
  • react-otp-input:

    Choose 'react-otp-input' if you are looking for a versatile OTP input solution that can be used in both web and mobile applications. It offers a simple API and is highly customizable, making it suitable for projects that require flexibility in design and functionality.

  • react-native-otp-entry:

    Choose 'react-native-otp-entry' if you need a library specifically designed for React Native applications, offering a seamless experience on mobile devices. It provides features like customizable styles and easy integration with mobile workflows, making it ideal for mobile-first applications.

README for react-otp-input

react-otp-input

npm version npm npm All Contributors

A fully customizable, one-time password input component for the web built with React.

see here

Live Demo

Installation

NPM

To install the latest stable version:

npm install --save react-otp-input

Still using v2?

No problem! You can find the documentation for v2 here

Basic usage:

import React, { useState } from 'react';
import OtpInput from 'react-otp-input';

export default function App() {
  const [otp, setOtp] = useState('');

  return (
    <OtpInput
      value={otp}
      onChange={setOtp}
      numInputs={4}
      renderSeparator={<span>-</span>}
      renderInput={(props) => <input {...props} />}
    />
  );
}

API

You can also pass a function that returns a component, where the function will get the index of the separator being rendered as an argument.
Name
TypeRequiredDefaultDescription
numInputsnumbertrue4Number of OTP inputs to be rendered.
renderInputfunctiontruenoneA function that returns the input that is supposed to be rendered for each of the input fields. The function will get two arguments: inputProps and index. inputProps is an object that contains all the props that should be passed to the input being rendered (Overriding these props is not recommended because it might lead to some unexpected behaviour). index is the index of the input being rendered.
onChangefunctiontrueconsole.logReturns OTP code typed in inputs.
onPastefunctionfalsenoneProvide a custom onPaste event handler scoped to the OTP inputs container. Executes when content is pasted into any OTP field.

Example:
const handlePaste: React.ClipboardEventHandler = (event) => {
  const data = event.clipboardData.getData('text');
  console.log(data)
};
valuestring / numbertrue''The value of the OTP passed into the component.
placeholderstringfalsenoneSpecify an expected value of each input. The length of this string should be equal to numInputs.
renderSeparatorcomponent / function
falsenoneProvide a custom separator between inputs by passing a component. For instance, <span>-</span> would add - between each input.
containerStylestyle (object) / className (string)falsenoneStyle applied or class passed to container of inputs.
inputStylestyle (object) / className (string)falsenoneStyle applied or class passed to each input.
inputType<input> typefalsetextThe type of the input that will be passed to the input element being rendered. In v2 isInputNum used to set the input type as tel and prevented non numerical entries, so as to avoid the spin buttons added to the inputs with input type number. That behaviour is still supported if you pass tel to the inputType prop.
shouldAutoFocusbooleanfalsefalseAuto focuses input on initial page load.
skipDefaultStylesbooleanfalsefalseThe component comes with default styles for legacy reasons. Pass true to skip those styles. This prop will be removed in the next major release.

⚠️ Warning

Do not override the following props on the input component that you return from the renderInput prop. Doing so might lead to unexpected behaviour.

  • ref
  • value
  • onChange
  • onFocus
  • onBlur
  • onKeyDown
  • onPaste
  • onInput
  • type
  • inputMode

Migrating from v2

The v3 of react-otp-input is a complete rewrite of the library. Apart from making the API more customizable and flexible, this version is a complete rewrite of the library using TypeScript and React Hooks. Here are the breaking changes that you need to be aware of:

  • You now need to pass your own custom input component that will be rendered for each of the input fields via renderInput prop. This gives you the flexibility to customize the input fields as you desire. This also means that props like focusStyle, isDisabled, disabledStyle, hasErrored, errorStyle, isInputNum, isInputSecure, data-cy and data-testid are no longer supported. You can achieve the same functionality and more by passing the relevant props directly to the input component that you return from the renderInput prop.

  • The separator prop has now been renamed to renderSeparator. This prop now apart from accepting a component that will be rendered as a separator between inputs like it used to, now also accepts a function that returns a component. The function will get the index of the separator being rendered as an argument.

  • A new prop called inputType has been added to the component. This prop can be used to specify the type of the input that will be passed to the input element being rendered. The default value of this prop is number.

Migrating from v1

react-otp-input is now a controlled component to facilitate functionalities that weren't possible before from the application using it, such as clearing or pre-assigning values. For v1.0.0 and above, a value prop needs to be passed in the component for it to function as expected.

Development

To run the vite example:

cd example
npm run dev

Checklist

PRs Welcome Open Source Love

  • [ ] Write tests
  • [ ] Add actions for lint checks and tests

Contributing

GitHub issues GitHub pull requests

Feel free to open issues and pull requests!

License

NPM

Contributors ✨

Thanks goes to these wonderful people (emoji key):

Abhishek Warokar
Abhishek Warokar

💻 🎨 🚧 🤔 👀
Aj
Aj

💻 🎨 🤔
Aromal Anil
Aromal Anil

💻 🔧
Gabriele Corti
Gabriele Corti

💻 ️️️️♿️
Anoop
Anoop

💻
Dewansh Rawat
Dewansh Rawat

🐛
Ishan Chhabra
Ishan Chhabra

💻
yaojie
yaojie

💻
Prateek Surana
Prateek Surana

💻 🚧 🤔 📖 🎨
Süleyman Barış Eser
Süleyman Barış Eser

🐛
Steve Domino
Steve Domino

💻

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