Has*_*sen 7 textinput react-native
我在facebook React Native页面上找到了示例代码,该页面显示了如何使用setNativeProp清除单击上的文本,但我看不到如何使用多个文本框进行操作.这是代码:
var App = React.createClass({
clearText() {
this._textInput.setNativeProps({text: ''});
},
render() {
return (
<View style={styles.container}>
<TextInput ref={component => this._textInput = component}
style={styles.textInput} />
<TouchableOpacity onPress={this.clearText}>
<Text>Clear text</Text>
</TouchableOpacity>
</View>
);
}
});
Run Code Online (Sandbox Code Playgroud)
ref似乎在函数中被修复,因此将始终以相同的TextInput框为目标.如何更改函数以定位我指示的任何TextInput框?
这应该工作.请注意,TextInput上的引用必须是您从clearText functino调用的引用.
var App = React.createClass({
clearText(fieldName) {
this.refs[fieldName].setNativeProps({text: ''});
},
render() {
return (
<View style={styles.container}>
<TextInput ref={'textInput1'} style={styles.textInput} />
<TouchableOpacity onPress={() => this.clearText('textInput1')}>
<Text>Clear text</Text>
</TouchableOpacity>
<TextInput ref={'textInput2'} style={styles.textInput} />
<TouchableOpacity onPress={() => this.clearText('textInput2')}>
<Text>Clear text</Text>
</TouchableOpacity>
</View>
);
}
});
Run Code Online (Sandbox Code Playgroud)
更新了我的答案以清除不同的字段.
归档时间: |
|
查看次数: |
6585 次 |
最近记录: |