Reactjs 在按下时将值传递给TextInput

Reactjs 在按下时将值传递给TextInput,reactjs,react-native,ecmascript-6,react-native-text,Reactjs,React Native,Ecmascript 6,React Native Text,我的组成部分是: class TextInputComp extends Component { constructor(props){ super(); this.state = { thetext: '' } } submitText = (text) => { Alert.alert("Text Submitted!", text); } render() { const renData = this.props.people(

我的组成部分是:

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>
    );
  }
}
class TextInputComp扩展组件{
建造师(道具){
超级();
this.state={文本:'}
}
submitText=(文本)=>{
Alert.Alert(“文本已提交!”,Text);
}
render(){
const renData=this.props.people((数据、索引)=>{
返回(
ID:{data.ID}-名称:{data.Name}
this.refs.MyBox.focus()}>
打开
)
});
返回(
{renData}
this.setState({thetext})}
/>
this.submitText(this.state.thetext)}>
提交
);
}
}
单击
打开
时,我可以在
文本输入上显示
{redData}
焦点
中的数据。我想将一个值传递给
TextInput
。假设我想传递
数据.name
,那么当
打开
时,我想
数据.name
已经在
文本输入的开头,这样我就可以将它传递给
this.state.thetext


我怎样才能做到这一点?非常感谢。

您可以控制
文本输入。为此,您可以像这样将
value
prop传递给
TextInput

<TextInput
  ref='MyBox'
  style={{height: 40}} 
  value={this.state.thetext}
  onChangeText={(thetext) => this.setState({thetext})}
  />
<TouchableOpacity
   onPress={()=>{this.refs.MyBox.focus(); this.setState({thetext: data.name})}} >
   <Text>Open</Text>
</TouchableOpacity>

您可以控制
文本输入。为此,您可以像这样将
value
prop传递给
TextInput

<TextInput
  ref='MyBox'
  style={{height: 40}} 
  value={this.state.thetext}
  onChangeText={(thetext) => this.setState({thetext})}
  />
<TouchableOpacity
   onPress={()=>{this.refs.MyBox.focus(); this.setState({thetext: data.name})}} >
   <Text>Open</Text>
</TouchableOpacity>

谢谢我确实在
TextInput
中获得了值,但它不允许我在其中添加任何内容。它只有传递的值。我尽量多打字,它不让我。这是为什么?哦。。没有关系。。我很抱歉。有个打字错误。现在开始工作了。非常感谢。我确实在
TextInput
中获得了值,但它不允许我在其中添加任何内容。它只有传递的值。我尽量多打字,它不让我。这是为什么?哦。。没有关系。。我很抱歉。有个打字错误。现在开始工作了。非常感谢你。