2015-07-02 35 views
5

Sto tentando di creare un elemento di input di testo ma continua a generare errore dicendo "Impossibile trovare la variabile: TextInput" anche se ho copiato il codice dalla pagina iniziale.React Native - Impossibile creare l'input di testo

Codice:

/** 
* Sample React Native App 
* https://github.com/facebook/react-native 
*/ 
'use strict'; 

var React = require('react-native'); 
var { 
    AppRegistry, 
    StyleSheet, 
    Text, 
    View, 
} = React; 

var AwesomeProject = React.createClass({ 
    render: function() { 
    return (
     <View style={styles.container}> 
     <Text style={styles.welcome}> 
      My App Name 
     </Text> 
     <TextInput 
      style={{height: 40, borderColor: 'gray', borderWidth: 1}} 
      onChangeText={(text) => this.setState({input: text})} 
     /> 
    <Text>{'user input: ' + this.state.input}</Text> 
     </View> 
    ); 
    } 
}); 

var styles = StyleSheet.create({ 
    container: { 
    flex: 1, 
    justifyContent: 'center', 
    alignItems: 'center', 
    backgroundColor: '#F5FCFF', 
    }, 
    welcome: { 
    fontSize: 20, 
    textAlign: 'center', 
    margin: 10, 
    }, 
    instructions: { 
    textAlign: 'center', 
    color: '#333333', 
    marginBottom: 5, 
    }, 
}); 

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

e l'immagine di errore allegato. enter image description here

risposta

20

è necessario inizializzare TextInput con il resto dei componenti reagiscono-native come segue:

var { 
    TextInput, 
    AppRegistry, 
    StyleSheet, 
    Text, 
    View, 
} = React; 
+0

solo aggiungere su questo: se hai dimenticato di importare TextInput in un altro file (ad esempio AnotherThing.js) che viene importato in App.js, l'errore sembrerà provenire da App.js anche se proviene da AnotherThing.js – nemicolopterus