【问题标题】:No `render` method found on the returned component instance在返回的组件实例上没有找到 `render` 方法
【发布时间】:2018-11-27 05:17:55
【问题描述】:

我已经通过命令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)

我的应用程序在这张图片中的代码和错误

即使在删除 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"
  }
}

【问题讨论】:

  • 请将代码作为文本内嵌到您的问题中,而不是图像
  • 这就是您应用中的所有代码吗?我刚刚运行它,它可以工作.. 你有一个名为 AppContainer 的类吗?
  • 您使用的是哪个 RN 版本?
  • 不,@kivul 我没有任何名为 AppContainer 的类
  • @LastBreath 我正在使用"react-native": "0.57.5"

标签: javascript react-native console.log


【解决方案1】:

我试过你的代码,它对我来说非常好。

也许您正面临缓存问题,试试这个:删除node_modules 文件夹并关闭打包程序。然后使用项目目录中的命令npm install 重新安装。

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

【讨论】:

  • 我已经提到过,删除 node_modules 并重新安装它们并不能解决问题
猜你喜欢
  • 2021-05-20
  • 2023-04-05
  • 2021-11-19
  • 2018-04-25
  • 2011-06-14
  • 1970-01-01
  • 2011-12-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多