Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/react-native/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
React native 如何在React Native中检索在按钮点击按下时输入的文本值?_React Native - Fatal编程技术网

React native 如何在React Native中检索在按钮点击按下时输入的文本值?

React native 如何在React Native中检索在按钮点击按下时输入的文本值?,react-native,React Native,我对React Native不太熟悉,无法逐个检索我在textInput中输入的内容 示例:如果我输入了一些名称,它应该一个接一个地出现 state = { name: "", showName: false }; buttonClickListner = e => { const { showName } = this.state; this.setState({ showName: true }); }; render() {

我对React Native不太熟悉,无法逐个检索我在textInput中输入的内容

示例:如果我输入了一些名称,它应该一个接一个地出现

  state = {
    name: "",
    showName: false
  };

  buttonClickListner = e => {
    const { showName } = this.state;

    this.setState({ showName: true });
  };

  render() {
    const { name } = this.state;
    return (
        <TextInput
          style={{ height: 150 }}
          placeholder="Enter a Name...."
          value={name}
          onChangeText={val => {
            this.setState({
              name: val
            });
          }}
        />
        <Button
          onPress={this.buttonClickListner}
          title="Submit"
          color="#008000"
        />
        <Text>{(showName = true ? name : null)}</Text>    
  );
  }
}
状态={
姓名:“,
showName:false
};
buttonClickListner=e=>{
const{showName}=this.state;
this.setState({showName:true});
};
render(){
const{name}=this.state;
返回(
{
这是我的国家({
姓名:val
});
}}
/>
{(showName=true?name:null)}
);
}
}

以下是我看到的一些要点:

  • 首先,您应该从return中返回一个单根,或者返回多个带有片段的元素(readfragments)。所以把它们都包装在一个标签中,比如//您的组件等等

  • 您正在
    showName=true
    中赋值,而不是检查。使用
    showName===true
    ?语法

以及完整的代码和:

import*as React from'React';
从“react native”导入{按钮,文本输入,文本,视图};
导出默认类App扩展React.Component{
状态={
姓名:“,
showName:false
};
buttonClickListner=e=>{
const{showName}=this.state;
this.setState({showName:true});
};
render(){
const{name,showName}=this.state;
返回(
{
这是我的国家({
姓名:val
});
}}
/>
{(showName==true?name:null)}
);
}
}

我已经做了你需要的。您需要一个名称序列,无论您在文本输入中键入什么,但使用字符串是不可能的。需要使用数组来显示要输入的所有值

export default class App extends React.Component {
   state = {
    name: [],
    showName: false,
    text:""
  };

  buttonClickListner = e => {
    this.state.name.push( this.state.text.toString() );
    this.setState({text:""})
  };
  render() {
    const { name } = this.state;
    return (
      <View>
        <TextInput
          style={{ height: 150 }}
          placeholder="Enter a Name...."
          value={this.state.text}
          onChangeText={val => {
            this.setState({
              text: val
            });
          }}
        />
        <Button
          onPress={this.buttonClickListner}
          title="Submit"
          color="#008000"
        />
        {this.state.name.length>0?this.state.name.map(item=>(<Text>{item}</Text>)):null} 
        </View>
  );
}
}
导出默认类App扩展React.Component{
状态={
姓名:[],
showName:false,
案文:“”
};
buttonClickListner=e=>{
this.state.name.push(this.state.text.toString());
this.setState({text:})
};
render(){
const{name}=this.state;
返回(
{
这是我的国家({
文本:val
});
}}
/>
{this.state.name.length>0?this.state.name.map(item=>({item})):null}
);
}
}

一个接一个的方法??