React Native - 无法创建文本输入

时间:2015-07-02 20:09:20

标签: reactjs react-native

我正在尝试创建一个文本输入元素,但它仍然抛出错误,说“无法找到变量:TextInput”,即使我从他们的入门页面复制了代码。

代码:

/**
 * 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);

并附上错误的图像。 enter image description here

2 个答案:

答案 0 :(得分:23)

您需要使用其余的react-native组件初始化TextInput,如下所示:

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

答案 1 :(得分:0)

我遇到了同样的问题。我做了什么!我删除了node-module,然后运行npm installyarn install。然后问题解决了。你可以试试看。

相关问题