0

I am a newcomer to react native and struggling with the following issue. I am doing an update function, once a user is selected then this user's data is populated into their dedicated fields TextInput like given_name, family_name and so on. The problem is when I try to modify the populated data from any TextInput it won't allow me as the text will not change at all and remain fixed as what it is no matter what. I am aware this is happening because the value of my fields are assigned to the incoming data that I want to update but how do I fix that and be able to modify this value ?

I would appreciate if you can help.

constructor(props) {
  super(props);
  this.state = {
    given_name: '',
    family_name: '',
    email: '',
    password: '',
    UserInfo: '',
  };
}

handleGivenName = (text) => {
  this.setState({
    given_name: text
  })
}
handleLastName = (text) => {
  this.setState({
    family_name: text
  })
}
handleEmail = (text) => {
  this.setState({
    email: text
  })
}
handlePass = (text) => {
  this.setState({
    password: text
  })
}

<TextInput placeholder="Last Name"
           onChangeText={ this.handleLastName }
           value={ this.state.UserInfo.family_name } />
6
  • What does your <TextInput /> component code looks like? Commented Mar 13, 2020 at 16:32
  • <TextInput placeholder="Last Name" onChangeText={this.handleLastName} value = {this.state.UserInfo.family_name} /> Commented Mar 13, 2020 at 16:34
  • @Khaled I think palaѕн wanted to know what the component definition of TextInput looks like? Commented Mar 13, 2020 at 16:36
  • component definition ! sorry what is that ? Commented Mar 13, 2020 at 16:43
  • @Khaled It's where the TextInput component is defined. Not your custom instance. See: TextInput.js Commented Mar 13, 2020 at 16:48

2 Answers 2

1

Try to use an arrow-function in onChangeText:

handleLastName(text) {
  this.setState({
    family_name: text,
    userInfo: { ...this.state.userInfo, family_name: text}
  })
}

render():

<TextInput   
placeholder="Last Name" 
onChangeText={(text) => this.handleLastName(text)} 
value={this.state.family_name} // remove UserInfo
/>
Sign up to request clarification or add additional context in comments.

1 Comment

The populated data is stored in UserInfo, if I removed it the textinput would be empty @Tim
0

you are reading value from a property this.state.UserInfo.family_name and update another one in change handler this.setState({ family_name: text }) that makes your value read-only; either update change handler or the value you are passing to the TextInput; your code is similar to this :

state = {
 a: 1,
 b: 2
}

changeHandler(e){
setState( { b: e.value } )

render(){
 <TextInput value={b} />
   ....

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.