在返回的组件实例上找不到`render`方法

时间:2018-11-27 05:17:56

标签: javascript react-native console.log

我已经通过命令react-native init DummyProject初始化了基本的react-native应用程序,并尝试编译并运行它。但它在调试控制台上给了我一个错误

Warning: AppContainer(...): No `render` method found on the returned component instance: you may have forgotten to define `render`.

TypeError: instance.render is not a function

This error is located at:
    in AppContainer (at renderApplication.js:33)

我的应用程序在此图中的代码和错误 The code and error my application is in this picture

即使删除了node_modules并再次安装了模块,我仍然遇到此错误。 制作另一个应用程序也不起作用。在每种情况下都会发生相同的错误

我的App.js代码

import React, { Component } from "react";
import { Text, View } from "react-native";

export default class App extends Component {
  constructor() {
    super();
    this.state = {
      value: 1
    };
  }
  render() {
    return (
      <View>
        <Text>HELLO {this.state.value}</Text>
      </View>
    );
  }
}

index.js

import { AppRegistry } from "react-native";
import App from "./App";
import { name as appName } from "./app.json";

AppRegistry.registerComponent(appName, () => App);

package.json

{
  "name": "DummyProject",
  "version": "0.0.1",
  "private": true,
  "scripts": {
    "start": "node node_modules/react-native/local-cli/cli.js start",
    "test": "jest"
  },
  "dependencies": {
    "react": "16.6.1",
    "react-native": "0.57.5"
  },
  "devDependencies": {
    "babel-jest": "23.6.0",
    "jest": "23.6.0",
    "metro-react-native-babel-preset": "0.49.2",
    "react-test-renderer": "16.6.1"
  },
  "jest": {
    "preset": "react-native"
  }
}

1 个答案:

答案 0 :(得分:0)

我尝试了您的代码,对我来说它工作得很好。

也许您遇到缓存问题,请尝试以下操作:删除node_modules文件夹并关闭打包程序。然后从您的项目目录中使用命令npm install重新安装。

之后,请尝试再次运行您的代码。它应该工作。