最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - pass multiple params onChangeText input element function - React native - Stack Overflow

programmeradmin3浏览0评论

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);
}

//.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.

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.

Share Improve this question asked Jan 4, 2017 at 15:40 splunksplunk 6,82517 gold badges64 silver badges109 bronze badges 3
  • 1 facebook.github.io/react-native/docs/handling-text-input.html - it doesn't appear to have the 2nd param. – G0dsquad 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 – Shubham Khatri Commented Jan 4, 2017 at 15:52
  • How can I solve this? – splunk Commented Jan 4, 2017 at 16:09
Add a ment  | 

2 Answers 2

Reset to default 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> );
}
handleChange= (name) => {
return (text) => {
   console.log("test: "name+" "+text); 
}};

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

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

评论列表(0)

  1. 暂无评论