How do you style a TextInput in react native for password input

React Native

React Native Problem Overview


I have a TextInput. Instead of showing the actual text entered, when the user enters text I want it to show the password dots / asterisks (****) you typically see in apps when typing a password. How can I do this?

<TextInput
  style={{ height: 40, borderColor: 'gray', borderWidth: 1 }}
  onChangeText={(text) => this.setState({input: text})}
/>

React Native Solutions


Solution 1 - React Native

When this was asked there wasn't a way to do it natively, however this will be added on the next sync according to this pull request. Here is the last comment on the pull request - "Landed internally, will be out on the next sync"

When it is added you will be able to do something like this

<TextInput secureTextEntry={true} style={styles.default} value="abc" />

refs

Solution 2 - React Native

May 2018 react-native version 0.55.2

This works fine:

secureTextEntry={true}

And this does not work anymore:

password={true} 

Solution 3 - React Native

Just add the line below to the <TextInput>

secureTextEntry={true}

Solution 4 - React Native

Add

secureTextEntry={true}

or just

secureTextEntry 

property in your TextInput.

Working Example:

<TextInput style={styles.input}
           placeholder="Password"
           placeholderTextColor="#9a73ef"
           returnKeyType='go'
           secureTextEntry
           autoCorrect={false}
/>

Solution 5 - React Native

I had to add:

secureTextEntry={true}

Along with

password={true}

As of 0.55

Solution 6 - React Native

An TextInput must include secureTextEntry={true}, note that the docs of React state that you must not use multiline={true} at the same time, as that combination is not supported.

You can also set textContentType={'password'} to allow the field to retrieve credentials from the keychain stored on your mobile, an alternative way to enter credentials if you got biometric input on your mobile to quickly insert credentials. Such as FaceId on iPhone X or fingerprint touch input on other iPhone models and Android.

 <TextInput value={this.state.password} textContentType={'password'} multiline={false} secureTextEntry={true} onChangeText={(text) => { this._savePassword(text); this.setState({ password: text }); }} style={styles.input} placeholder='Github password' />
   

  

Solution 7 - React Native

A little plus:

version = RN 0.57.7

secureTextEntry={true}

does not work when the keyboardType was "phone-pad" or "email-address"

Solution 8 - React Native

<TextInput
  placeholderTextColor={colors.Greydark}
  placeholder={'Enter Password'}
  secureTextEntry={true} />

Solution 9 - React Native

You can get the example and sample code at the official site, as following:

<TextInput password={true} style={styles.default} value="abc" />

Reference: http://facebook.github.io/react-native/docs/textinput.html

Solution 10 - React Native

If you added secureTextEntry={true} but did not work then check the multiline={true} prop, because if it is true, secureTextEntry does not work.

Solution 11 - React Native

<TextInput 
   secureTextEntry 
   placeholder="password" placeholderTextColor="#297542" 
   autoCorrect={false} style={mystyles.inputStylee} 
/>

You can simply add the secureTextEntry prop to TextInput and omit its value. By default, it is set to true. To make it to false do this secureTextEntry={false}

Solution 12 - React Native

 <TextInput
        placeholder="Password"
        secureTextEntry={true}
        style={styles.input}
        onChangeText={setPassword}
        value={password}
      />

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionbwbrowningView Question on Stackoverflow
Solution 1 - React NativeRiley BrackenView Answer on Stackoverflow
Solution 2 - React NativemediaguruView Answer on Stackoverflow
Solution 3 - React NativeCurious96View Answer on Stackoverflow
Solution 4 - React NativeRohan KView Answer on Stackoverflow
Solution 5 - React NativeNicholasByDesignView Answer on Stackoverflow
Solution 6 - React NativeTore AurstadView Answer on Stackoverflow
Solution 7 - React NativeqloveshmilyView Answer on Stackoverflow
Solution 8 - React NativeAntier SolutionsView Answer on Stackoverflow
Solution 9 - React NativeRichard LiView Answer on Stackoverflow
Solution 10 - React NativeMahmut GundogduView Answer on Stackoverflow
Solution 11 - React NativechandruView Answer on Stackoverflow
Solution 12 - React NativeRavindu SathsaraView Answer on Stackoverflow