React native AppRegistry不是可调用模块

时间:2017-10-08 11:40:25

标签: javascript ios react-native

我在“学习反应原生O Reilly”教程中遇到一个小问题。

我在第一个应用程序(WeatherProject)上,当我执行react-native init WeatherProject命令时,奇怪的是index.ios.js和index.android.js没有自动创建?我认为这是正常的。所以我创建了它们,并填写如下:

var React = require('react-native'); var { AppRegistry } = React; var WeatherProject = require('./WeatherProject'); AppRegistry.registerComponent('WeatherProject', () => WeatherProject);

然后我将WeatherProject.js文件如下:

import React, { Component } from "react";

import { StyleSheet, Text, View, TextInput } from "react-native";

class WeatherProject extends Component {
  constructor(props) {
    super(props);
    this.state = { zip: "" };
  }

  _handleTextChange = event => {
    this.setState({ zip: event.nativeEvent.text });
  };

  render() {
    return (
      <View style={styles.container}>
        <Text style={styles.welcome}>
          You input {this.state.zip}.
        </Text>
        <TextInput
          style={styles.input}
          onSubmitEditing={this._handleTextChange}
        />
      </View>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: "center",
    alignItems: "center",
    backgroundColor: "#F5FCFF"
  },
  welcome: { fontSize: 20, textAlign: "center", margin: 10 },
  input: {
    fontSize: 20,
    borderWidth: 2,
    padding: 2,
    height: 40,
    width: 100,
    textAlign: "center"
  }
});

export default WeatherProject;

并将index.js文件设置如下:

import WeatherProject from "./WeatherProject";

export default WeatherProject;

我跟随的书已经过时了,还是我做了一些愚蠢的事情?但是当我在iphone模拟器上运行应用程序时,我收到错误“AppRegistry不是可调用的模块。(调用runApplication)”

1 个答案:

答案 0 :(得分:0)

您正在导入错误的AppRegistry并做出反应。

尝试在index.ios.js和index.android.js中导入它们,如下所示:

import React from 'react';
import { AppRegistry } from 'react-native';
import WeatherProject from './WeatherProject';

这应该有用。

您应该查看官方文档,这是AppRegistry.registerComponent的示例 https://facebook.github.io/react-native/docs/props.html#content