在React Native中按下按钮时,逐个追加TextInput组件

2023-12-04

我是新来的React Native.

我使用两个按钮来添加TextInput视图中的组件。 当我按下Add按钮,按下一个TextInput组件进入视图,当我按下Add Again按钮,它会推动另一个TextInput组件位于前一个组件下方。

但当我按下Add再次按钮,它会按下TextInput第一个组件下方。但我想把它推到最后一个以下。

Like : Add |Add | Add again.

但是我需要Add | Add Again | Add等等。

可以采取什么措施来实现这一目标? 我已关注this.

'use strict';

import React, {Component} from 'react';
import {
  AppRegistry,
  StyleSheet,
  Text,
  View,
  TouchableHighlight,
  TextInput,
 } from 'react-native';

let index = 0 

class Test extends Component{
constructor(){
super();
this.state = {
  arr: []
};
}
insertSomeThing(){
this.state.arr.push(index++)
this.setState({ arr: this.state.arr })
}

render() {
let arr = this.state.arr.map((r, i) => {
    return (
      <View key={ i } style={styles.insertStyle}>
        <TextInput placeholder = 'abc' />
      </View>
      );
})
let arrAnother = this.state.arr.map((r, i) => {
    return (
      <View key={ i } style={styles.insertStyle}>
        <TextInput placeholder = 'def' />
      </View>
      );
})

return (
  <View style={styles.container}>
  <View>
    <TouchableHighlight 
      onPress={ this.insertSomeThing.bind(this) } 
      style={styles.button}>
        <Text>Add</Text>
    </TouchableHighlight>
  </View>
  <View>
    <TouchableHighlight 
      onPress={ this.insertSomeThing.bind(this) } 
      style={styles.button}>
        <Text>Add Again</Text>
    </TouchableHighlight>
  </View>
  { arr }
  {arrAnother}
  </View>
);
}
}

var styles = StyleSheet.create({
  container: {
flex: 1,
marginTop: 60,
  }, 
  insertStyle: {
height:40,
alignItems: 'center',
justifyContent: 'center',
borderBottomColor: '#ededed',
borderBottomWidth: 1
  },
  button: {
alignItems: 'center',
justifyContent: 'center',
height:55,
backgroundColor: '#ededed',
marginBottom:10
  }
});

AppRegistry.registerComponent('Test', () => Test);

问题是你正在打电话insertSomeThing函数和这个函数推动了一个新的<Text>在变量中arr然后按以下顺序渲染:

</View>
    { arr }
    {arrAnother}
</View>

如果您想在列表末尾插入不同的 TextView,则必须删除{arrAnother}并修改您的地图功能。您的代码将如下所示:

insertSomeThing( placeholder ){
  this.state.arr.push({index:index++, placeholder:placeholder});
  this.setState({ arr: this.state.arr });
}

render() {
  let arr = this.state.arr.map((r, i) => {
    return (
      <View key={ i } style={styles.insertStyle}>
        <TextInput placeholder = {r.placeholder} />
      </View>
    );
  });

  return (
    <View style={styles.container}>
      <View>
        <TouchableHighlight 
          onPress={ () => this.insertSomeThing('add') } 
          style={styles.button}>

          <Text>Add</Text>
        </TouchableHighlight>
      </View>
      <View>
        <TouchableHighlight 
          onPress={ () => this.insertSomeThing('addAgain') } 
          style={styles.button}>
             <Text>Add Again</Text>
        </TouchableHighlight>
      </View>
      { arr }
    </View>
  );
}

EDIT

处理onChangeText你的 TextInput 看起来像这样:

let arr = this.state.arr.map((r, i) => {

    var ref = 'textinput'+i;

    return (
      <View key={ i } style={styles.insertStyle}>
        <TextInput ref={ref} onChangeText={(text) => this._onChangeText(text,ref)} />
      </View>
    );
  });

你必须定义_onChangeText在您的组件中处理该事件。您将收到文本和输入参考。您稍后可以使用引用输入this.refs[ref].

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在React Native中按下按钮时,逐个追加TextInput组件 的相关文章

随机推荐