react-native - 修改index.js后黑屏

标签 react-native

我正在按照在线教程学习如何使用 React。

老师让我创建了一个名为albums的项目并修改了index.js的内容,但是我在ios模拟器上黑屏了。

我做了什么(根据讲师的详细信息):

1) 创建一个新项目react-native init-albums

2)进入项目目录cd albums

3) 运行react-native run-ios

4) 我可以在模拟器屏幕上看到文件 App.js 中的内容(初始屏幕 - 我假设 - 任何新的 React Native 项目)。 按 Cmd+R 重新加载Cmd+D 或摇动开发菜单

5)删除index.js里面的内容,替换为:

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

const App = () => {
 return <Text>Some Text</Text>;
};

AppRegistry.registerComponent("albums", () => App);

它应该出现在模拟器的左上角 Some Text 但它没有。屏幕是黑色的。 我做错了什么?

最佳答案

您需要为您的应用程序定义背景颜色。您还应该从 react-native 导入 View

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

const App = () => {
 return (
   <View style={{backgroundColor: 'white', flex:1}}>
     <Text>Some Text</Text>
   </View>
  );
};

它是黑色的原因是因为 AppDelegate.m 中的 rootView backgroundColor 在版本 0.58.0

中被改变了

在以前的版本中是

rootView.backgroundColor = [[UIColor alloc] initWithRed:1.0f green:1.0f blue:1.0f alpha:1];

现在是0.58.+

中的以下内容
rootView.backgroundColor = [UIColor blackColor];

关于react-native - 修改index.js后黑屏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54931787/

相关文章:

java - 如何在 React Native 中从 Java 代码访问 ReactContext?

android - React Native - Dimensions.set() 不起作用?

android - 如何解决 react-native 中的 android Overflow 问题

javascript - 防止用户删除 TextInput 上的默认值

android - 在Android上运行React Native项目

javascript - 添加类来 react div onClick

javascript - Create React App 提供的 react-scripts 包需要依赖 :

react-native - 打印 : Entry, ":CFBundleIdentifier",不存在......我已经尝试了一切

react-native - 以下文件共享它们的名称;请调整你的 hasteImpl : * <rootDir>\package. json * <rootDir>\sanity\package.json

javascript - 下一次按下时关闭 React-Native 中的下拉菜单