Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Next.js and React

TextInput warns that value is a number but a string is expected

ErrorWarning: Failed prop type: Invalid prop `value` of type `number` supplied to `TextInput`, expected `string`

Short answer

The value prop of TextInput is typed as a string. If state holds a number, React Native warns that it expected a string. Keep the typed text as a string in state, show that string in value, and convert to a number only when you calculate or submit.

  • Applies to React Native (current), Expo (managed), React 19
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Find every TextInput whose value is not a string, and see what the state behind it starts as.

    grep -rn -B1 -A4 "<TextInput" src app 2>/dev/null | grep -E "value=|useState"
  2. 2Match what you see, then jump to its fix

    1. 1useState(0) or a number field from an API object feeds valueState is a number and is passed straight to valueFix
    2. 2The handler calls Number(), parseInt() or parseFloat() and stores the resultonChangeText converts the text to a number before saving itFix

What does the error look like?

  • Warning: Failed prop type: Invalid prop `value` of type `number` supplied to `TextInput`, expected `string`

Cause 1 of 2

State is a number and is passed straight to value

1Check

The React Native docs type TextInput value as string, and it is a controlled component, so what you pass is what the native field shows.

grep -rn -E "useState\((0|[0-9.]+)\)" src app 2>/dev/null

2Fix

Keep the field's text as a string in state. If the number comes from elsewhere, convert it with String() at the prop.

const [qty, setQty] = useState('1');

<TextInput value={qty} onChangeText={setQty} keyboardType="numeric" />

// number from a model object
<TextInput value={String(item.quantity)} onChangeText={...} />

3Verify

Reload the app; the warning should no longer print.

npx expo start --clear

Confirmed by reactnative.dev

Did this fix it?

Cause 2 of 2

onChangeText converts the text to a number before saving it

1Check

onChangeText passes the new text as one string. A number keyboard type shows digits, but the callback still gives a string, and the controlled value must be a string again on the next render.

grep -rn -E "onChangeText=.*(Number|parseInt|parseFloat)" src app 2>/dev/null

2Fix

Store the raw string. Convert when you need the number. Clean the text yourself if you want to block other characters.

const [amount, setAmount] = useState('');

<TextInput
  value={amount}
  keyboardType="decimal-pad"
  onChangeText={(t) => setAmount(t.replace(/[^0-9.]/g, ''))}
/>

const numeric = parseFloat(amount) || 0;

3Verify

Type digits, then clear the field; no warning appears and the field can be empty.

npx expo start --clear

Confirmed by reactnative.dev

Did this fix it?

How do I stop it coming back?

  • Type the state as string in TypeScript, so a number cannot reach value.
  • Parse text to numbers in one place, at submit or at calculation time.
  • Use keyboardType or inputMode for the keyboard and your own code for filtering characters, since the docs do not say the keyboard blocks other input.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Web apps

Expo app never leaves the splash screen

App shows the splash screen forever and no error appears

After SplashScreen.preventAutoHideAsync() the splash stays until you hide it yourself.

3 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.