Programmatically add a component in React Native

后端 未结 4 1623
忘掉有多难
忘掉有多难 2020-12-09 04:35

Suppose I have a simple React Native app like so:

\'use strict\';

var React = require(\'react-native\');
var {
  AppRegistry,
  Text,
  TouchableHighlight,
         


        
4条回答
  •  南方客
    南方客 (楼主)
    2020-12-09 05:19

    ECMA6 Syntax

    import React, { Component } from 'react';
    import {
    View,
    Text,
    StyleSheet,    
    TextInput,
    TouchableOpacity,
    TouchableHighlight
    } from 'react-native';    
    
    export default class fourD extends Component {
    
      constructor(props) {
        super(props);
        let ele1 = (
          
            Element {1}
             this._add()  }>
             Add
            
          
         );
    
        this.state = {
          ele: [],
          key: 1
        }
    
        this.state.ele.push(ele1);
    
       }
    
      _add(){
    
        let key = this.state.key + 1;
    
        let ele2 = (
          
            Element {key}
             this._add()  }>
             Add
            
          
        );
    
        let ele = this.state.ele;
        ele.push(ele2);
        this.setState({ ele: ele,key : key})
    
      }
      render() {
    
        return (
          
           This text should be before
            { this.state.ele }
           This text should be after
            this._add()  }>
              Tap Me
            
          
        )
      }
    }
    
    
    
    const styles = StyleSheet.create({
        container: {
          flex: 1,
          backgroundColor: "white",
        }
    })
    

提交回复
热议问题