Hi in my componentWillMount I set my states like this
componentWillMount(){
this.timeSheetData().then((timeSheetResponse)=>{
this.setState({comments:timeSheetResponse.comments});
this.setState({spentHours:parseInt(timeSheetResponse.hours)});
alert(this.state.spentHours);
});
});
In my view
I have the TextInput like this. I can't display the value on the TextInput but I can display the value on Text I don't know why it's happening
<TextInput keyboardType='numeric' onChangeText={(spentHours) =>this.setState({spentHours})}
value={this.state.spentHours} />
Input values need to be strings so you either need to remove parseInt in your componentWillMount method:
this.setState({ spentHours: timeSheetResponse.hours });
or in your template you need to convert the input value to a string with toString():
<TextInput
keyboardType='numeric'
onChangeText={spentHours => this.setState({ spentHours })}
value={this.state.spentHours.toString()}
/>
Related
I am trying to make an input form for a team that does not have a fixed number of players and it should be optional on how many you enter without limits.
The idea is to have a button that puts an empty string into an array of team players which then FlatList should react to without refreshing the page and give you fields for input which would store the value on change without a button.
So it would something like this:
const TeamContentScreen = ({navigation}) => {
const [list, setList] = useState([""]);
const addToList = () => {
let tempArr = list;
tempArr.push("");
setList(tempArr);
console.log(list);
console.log(list.length);
};
return(
<View>
<Button onPress={addToList} title={"+1 player"}/>
<FlatList
data={list}
keyExtractor = {(e,i)=> i.toString()}
renderItem={({item, index})=>{
return(
<TextInput
placeholder="Type player name here"
onChangeText={/*way to dynamically update the value of an element of this index*/}}
/>
)
}}
/>
)
}
export default TeamContentScreen;
Is this possible?
If yes, how do you suggest dynamically updating the input fields display without refreshing?
How would you suggest storing the inputted values in the array without a button?
Try this
const onChangeText = (value, index) => {
let newList = [...list]; // clone list to a new list
newList[index] = value; // change the value at the index of list
setList(newList); // set new list
}
<TextInput
placeholder="Type player name here"
onChangeText={(value) =>onChangeText(value, index) }
/>
I have a textinput like this:
<TextInput
secureTextEntry={true}
autoCompleteType="password"
onChangeText={val => setClave(val)}
/>
and the component is:
const [clave, setClave] = useState('');
which is just for the password, because it is a login screen.
So i press a button and execute some functions that validates the textInput data, and when is all ready, navigate to another screen,
my final function that navigates to another screen is this:
const goInicio = () => {
setClave('')
navigation.navigate('Home')
};
and what i tried to do there is to set the state of clave to an empty string but if i navigate back to the login screen the password will still be written, even though its value changed, and it no longer works to log in, it is still showing as if it has something written, how can i fix that?
The internal value state of TextInput and clave are unrelated since you have not set clave to be the value prop of TextInput. You are changing clave in the onChange method of the TextInput but not the other way around. This direction is one directional.
Instead, set the value prop of the TextInput to be equal to clave
<TextInput
value={clave}
secureTextEntry={true}
autoCompleteType="password"
onChangeText={val => setClave(val)}
/>
I don't know whether it's a bug on react native TextInput or if I am doing something wrong. When I try to show initial value based on state as default value on TextInput it works only for string and not for number.
this.state = {referralCode: 10}
<TextInput value={this.state.referralCode} />
This due to some reason does not works and shows empty but it should read the intezer value from the state. As soon as I update the state with some string as initial value the TextInput shows it on initial render.
this.state = {referralCode: 'Rishav'};
<TextInput value={this.state.referralCode}
/>
It happens to render the initial state value now because it is in string.
Also,
<TextInput value={10}/> fails but <TextInput value={'10'}/> works
According to documentation . Value props need string . if you want to use number set in state , you can do like this
constructor(props) {
super(props);
this.state = {
referralCode: 10
};
}
<TextInput
onChangeText={referralCode => this.setState(referralCode)}
value={`${this.state.referralCode}`}
blurOnSubmit={true}
autoCapitalize="none"
returnKeyType="done"
/>
I have one text input in which i'm gonna typing some name while user typing the name get translated and back to set in text Input field how can i do this
<TextInput
style={{color:'black',width:width-60, borderColor: 'black'}}
underlineColorAndroid='black'
placeholderTextColor="black"
onChangeText={ (textValue) => this.setState({
input: textValue}) }
/>
// TRANSLATED TEXT
<PowerTranslator
text={this.state.input} />
TextInput have prop named "value".
For setting data to text input, you should set "value" to state value.
<TextInput
value={this.props.input}
/>
https://facebook.github.io/react-native/docs/textinput.html#value
In your code, power translator view component only for displaying text.
If you look at this component page, you should use it as service for getting translated text.
Translation.get('Engineering physics or engineering science').then(translated => {
//Do something with the translated text });
In React Native, I want to pass the value of the TextInput in the onBlur event handler.
onBlur={(e) => this.validateText(e.target.value)}
e.target.value works for plain React. But, in react-native, e.target.value is undefined. What is the structure of event args available in React Native?
You should use the 'onEndEditing' method instead of the 'onBlur'
onEndEditing?: function Callback that is called when text input ends.
onBlur is a component function where onEndEditing is specific for TextInput
onEndEditing
This approach works for both multiline and single line.
<TextInput
onEndEditing={(e: any) =>
{
this.setState({textValue: e.nativeEvent.text})
}
}/>
In React Native, you can get the value of the TextInput from e.nativeEvent.text.
Unfortunately, this doesn't work for multiline={true}. One hack around this is to maintain a ref to your TextInput and access the text value through the _lastNativeText property of the component. For example (assuming you've assigned your TextInput component a ref of "textInput"):
onBlur={() => console.log(this.refs.textInput._lastNativeText)}
Simple solution:
This way onBlur your state's email will always have the last value changed by the user.
validate = () => {
const { email } = this.state
console.log('Validating Email Here!', email)
}
<TextInput
style={styles.input}
placeholder='E-mail'
onChangeText={email => this.setState({email})}
onBlur={e => this.validate()}
/>