Home > front end >  TextInput Placeholder when using props
TextInput Placeholder when using props

Time:01-05

I am new to react native. I trying to use props to pass values into TextInput. However the placeholder was not appearing. Anyone can help point out what have gone wrong ?

'''

import React from 'react';
import { View,Text, TextInput } from 'react-native';

function App(){
  return(
  <View style={{ flex: 1, justifyContent: 'center', backgroundColor: '#ecf0f1' }}>
   
  <InputWithLabel label="Emails"
  placeholder="add text here to see in place holder"
  placeholderTextColor="black"
  value=" " 
  />
  </View>
)};

function InputWithLabel(props){
  const {label,placeholder,value,onChangeText} = props;
  return (
    <View>
    <Text>{label}</Text>

    <TextInput 
    placeholder={placeholder}
    value={value} 
    onChangeText={onChangeText}/>
    </View>
  );
}
export default App;

'''

CodePudding user response:

Looks like the reason the placeholder is not showing is because the value is a single blank space. Try passing in null or an empty string to the value like this:

import React from 'react';
import { View,Text, TextInput } from 'react-native';

function App(){
  return(
  <View style={{ flex: 1, justifyContent: 'center', backgroundColor: '#ecf0f1' }}>
   
  <InputWithLabel label="Emails"
  placeholder="add text here to see in place holder"
  placeholderTextColor="black"
  value=""
  />
  </View>
)};

function InputWithLabel(props){
  const {label,placeholder,value,onChangeText} = props;
  return (
    <View>
    <Text>{label}</Text>

    <TextInput 
    placeholder={placeholder}
    value={value} 
    onChangeText={onChangeText}/>
    </View>
  );
}
export default App;

CodePudding user response:

You are sending value=" " prop which isn't empty, try sending empty prop like

<InputWithLabel
  .....
  .....
  value="" // this way 
/>
  •  Tags:  
  • Related