javascript - 如何在 react 中显示 Hello World ?

标签 javascript reactjs react-native react-router

我正在使用 react-native 使用 ES6 尝试显示“hello world”为什么它不显示 hello world 文本..

这是我的代码

https://rnplay.org/apps/zFnEdg

'use strict';

var React = require('react-native');
var {
  AppRegistry,
  StyleSheet,
  Text,
  View,
} = React;

class PropertyFinderApp extends React.Component {
  render() {
    return React.createElement(React.Text, {style: styles.text}, "Hello World!");
  }
}


AppRegistry.registerComponent('PropertyFinderApp', () => PropertyFinderApp);

最佳答案

这不起作用,因为您正在使用样式,而样式甚至没有定义。

此外,您还必须从 'react' 库导入 React,而不是从 'react-native' 导入 React,如 docs 中所示。

您的代码应如下所示。

import React, {
  Component,
} from 'react';

import {
  AppRegistry,
  Component,
  StyleSheet,
  Text,
  View
} from 'react-native';

const styles = StyleSheet.create({
    text: {
        fontSize: 20
    }
});


class PropertyFinderApp extends React.Component {
  render() {
    return (<View>
           <Text style={styles.text}> "Hello World!"</Text>
         </View>)
  }
}

AppRegistry.registerComponent('PropertyFinderApp', () => PropertyFinderApp);

这里是 rnplay 中的示例,请检查 Android 版本。

rnplay

关于javascript - 如何在 react 中显示 Hello World ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37359951/

相关文章:

javascript - 哪种 JavaScript 压缩器(粉碎器)与 Google 用于其 JavaScript API 的压缩器执行相同的操作?

javascript - Javascript 中的 {variable} 语句如何工作?

PHP FTP 和 ajax 或类似的

javascript - 尝试通过更新 Reactjs 中的状态来更改背景

react-native - 删除 native 基础中 <Form> 组件中的左间距

javascript - 如何使用 React Navigation 在 React Native 中显示标题标题

javascript - 在 Dynamics CRM 的帐户查找中显示特定帐户记录

css - 如何在 Enzyme 中测试 CSS 悬停 - React

javascript - 设置 Material UI Snackbar 的背景颜色

android - 如何设置 fable-elmish-react native 应用程序?