【发布时间】:2017-09-13 20:20:44
【问题描述】:
我的组件是:
class TextInputComp extends Component {
constructor(props){
super();
this.state = { thetext: '' }
}
submitText = (text) => {
Alert.alert("Text Submitted!", text);
}
render() {
const renData = this.props.people((data, index) => {
return (
<View key={index} style={styles.card}>
<Text style={styles.names}> ID: {data.id} - Name: {data.name} </Text>
<TouchableOpacity
onPress={()=>this.refs.MyBox.focus()} >
<Text>Open</Text>
</TouchableOpacity>
</View>
)
});
return (
<View style={mystyles1.container}>
{renData}
<View>
<TextInput
ref='MyBox'
style={{height: 40}}
onChangeText={(thetext) => this.setState({thetext})}
/>
<TouchableOpacity
onPress = { () => this.submitText(this.state.thetext) }>
<Text style = {styles.ButtonText}> Submit </Text>
</TouchableOpacity>
</View>
</View>
);
}
}
当点击Open 时,我可以在TextInput 上显示来自{redData} 和focus 的数据。我想将一个值传递给TextInput。假设我想传递data.name,所以当onPress 位于Open 上时,我希望data.name 已经在TextInput 的开头,这样我就可以将它传递给this.state.thetext。
我怎样才能做到这一点?非常感谢。
【问题讨论】:
标签: reactjs react-native ecmascript-6 react-native-text