3

I have multiple Input elements and everytime the user type into these Inputs I call handleChange() function which should tell what the user has written and which Input he has typed in.

This is what I have done:

handleChange(text, name) {
    console.log("test: "text+" "+name);
}

//http://facebook.github.io/react-native/releases/0.23/docs/textinput.html
for (var p = 0; p < 20; p++){
      products.push (<TextInput name={p} onChangeText={(text, name) => this.handleChange(text, name)}></TextInput> );
}

the console.log inside handleChange function correctly shows the text written by user but doesn't display correctly name variable which results undefined.

3
  • 1
    facebook.github.io/react-native/docs/handling-text-input.html - it doesn't appear to have the 2nd param. Commented Jan 4, 2017 at 15:46
  • onChangeText by default takes the parameter value as the text entered and hence only one param. What you can try is onChangeText{(text, p) => this.handleChange(text, p)}} and see if it works Commented Jan 4, 2017 at 15:52
  • How can I solve this? Commented Jan 4, 2017 at 16:09

2 Answers 2

9

What is name supposed to be? All you're doing is setting name to an integer, but here's a way to make it work:

handleChange(text, name) {
  console.log("test: "text+" "+name);
}

//http://facebook.github.io/react-native/releases/0.23/docs/textinput.html
for (let p = 0; p < 20; p++){
  products.push (<TextInput onChangeText={(text) => this.handleChange(text, p)}></TextInput> );
}
Sign up to request clarification or add additional context in comments.

3 Comments

If I do like you said everytime handleChange is called p is always equal to 19, the last loop index value. Instead I want this: If I change the first input "p" (the one passed in the params along with text) should be 1, if I change the second input "p" should be 2 and so on. In my code It seems that name={p} doesn't assign "p" to name. I tried with value={p} and inputs were still empty, while if I do value="a" all 20 inputs hold "a" content. How can I assign variable "p" to name and value of inputs?
Still don't understand what you mean, the text variable is whatever the user inputs into the textfield. Also if you change var p = 0 to let p = 0 that should solve your other issue.
Sorry my bad. I was doing something wrong. Deleted the comment.
0
handleChange= (name) => {
return (text) => {
   console.log("test: "name+" "+text); 
}};

[https://hackernoon.com/curry-away-in-react-7c4ed110c65a][1]

for (let p = 0; p < 20; p++){
products.push (<TextInput onChangeText={this.handleChange(p)}> 
</TextInput> );
}

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.