FG
📱 Mobile & Cross-Platform

Duplicated letters when autoCapitalize="characters" on android

Freshabout 19 hours ago
Mar 14, 20260 views
Confidence Score95%
95%

Problem

Description When trying to implement an upper case only input, adding `autoCapitalize="characters"` seems to work on Android by setting the keyboard to be upper case by default. One can hit the shift key and then type a lower case letter. To ensure that we only let the user enter (and see) upper case letters, I thought that I might be able to handle it by ensuring that when we update the react state of the component we capture the text in upper case. By using a toUpperCase on top of what is a pretty standard state update cycle (i.e. a very similar update method ala the examples at https://facebook.github.io/react-native/releases/next/docs/textinput.html ), this saves the input into the state, uppercased, ready for the next render cycle. (I'm not concerned about the dangers of toUpperCase at this point.) Unfortunately, the behaviour is a bit strange when you start typing both upper and lowercase letters, where you start getting repeated letters, e.g. if I type _AbC_, I will end up with _ABABC_, _AbcdE_ I get _ABABCDABABCDE_. Reproduction I created an example app here: https://rnplay.org/apps/t-gBOA Note that the behaviour seems fine on the iOS simulator, but is 'wrong' on the android simulator. or see the component below: [code block] Solution I suspect that there's something going awry with the syncing of state between the react state and the state of the underlying components, quite possibly some case-ignoring checks are being in some places, but not others. Addit

Unverified for your environment

Select your OS to check compatibility.

1 Fix

Canonical Fix
Unverified Fix
New Fix – Awaiting Verification

Fix Duplicated Letters in TextInput with autoCapitalize on Android

Medium Risk

The issue arises because the React state updates with uppercased text while the underlying TextInput component retains its own state. When the user types, the TextInput triggers an update that appends the new input, leading to duplicated letters. This happens because the TextInput's internal state does not sync correctly with the React state when autoCapitalize is set to 'characters'.

Awaiting Verification

Be the first to verify this fix

  1. 1

    Modify TextInput onChangeText Handler

    Update the onChangeText handler to prevent the default behavior of appending characters. Instead, set the state directly to the uppercase version of the input.

    typescript
    onChangeText={(text) => setText(text.toUpperCase())}
  2. 2

    Use value Prop in TextInput

    Ensure that the TextInput component uses the value prop to bind the state. This will ensure that the displayed value is always in sync with the React state.

    typescript
    <TextInput value={text} onChangeText={(text) => setText(text.toUpperCase())} autoCapitalize='characters' />
  3. 3

    Debounce State Updates

    Implement a debounce mechanism to limit the frequency of state updates. This can help reduce the chance of duplicated letters by ensuring that rapid input does not cause multiple state updates.

    typescript
    const debouncedSetText = useCallback(debounce((newText) => setText(newText), 300), []);
  4. 4

    Test on Android Emulator

    After implementing the changes, test the application on the Android emulator to ensure that the duplicated letters issue is resolved. Type a mix of upper and lower case letters to confirm.

Validation

Confirm that typing in the TextInput only displays uppercase letters without any duplication. Test with various combinations of upper and lower case letters to ensure consistent behavior.

Sign in to verify this fix

Environment

Submitted by

AC

Alex Chen

2450 rep

Tags

react-nativemobileiosandroidran-commandsresolution:-pr-submittedcomponent:-textinputplatform:-android