module - ReactNative - 未定义不是一个对象(评估 x)

标签 module react-native

我正在阅读有关 ReactNative 的在线教程,其中它将一个文件“包含”到项目中;几乎就像一个部分。

我收到此错误;

Picture of an error

我的index.ios.js

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

//var DayItem = require('./src/day-item');
import {
  DayItem
} from './src/day-item';


// Component
class Weekdays extends Component {
  render() {
    return (
      <View>
        <Text style={styles.textStyles}>
          Days of the Week
        </Text>
        <DayItem />
      </View>
    );
  }
}

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

day-item文件是:

// Imports
import React, { Component } from 'react';
import {
  AppRegistry,
  StyleSheet,
  Text
} from 'react'

// Component
class DayItem extends Component { 
    render() {
        return (
            <Text>
            Hello world
            </Text>
        );
    } 
}

var { NativeModules } = require('react-native');
module.exports = NativeModules.DayItem;

我以前没有做过React Native,但我不确定当它声称 undefined 不是一个对象时它指的是什么。

这个想法是将此模块包含到我的主项目中,并在一行中呈现 DayItem 模块。

我注意到有时我会遇到错误

<DayItem />

如果我把它放在 <View> 之外,但里面还好。

无论如何,我不确定如何让我的 Hello World Partial 在我的主文件中工作。

对此的任何帮助都会有所帮助。

现在谢谢

最佳答案

正如 Cherniv 和 Nader 指出的,代码中存在一些错误。

另一个错误是在 day-item js 文件中

import {
  AppRegistry,
  StyleSheet,
  Text
} from 'react';

应该是

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

这是完整的工作代码。

index.ios.js

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

import DayItem from './src/day-item';


// Component
class Weekdays extends Component {
  render() {
    return (
      <View>
        <Text>
          Days of the Week
        </Text>
        <DayItem />
      </View>
    );
  }
}

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

src/day-item.js

// Imports
import React, { Component } from 'react';
import {
  AppRegistry,
  StyleSheet,
  Text
} from 'react-native';

// Component
class DayItem extends Component {
    render() {
        return (
            <Text>
            Hello world
            </Text>
        );
    }
}

export default DayItem;

关于module - ReactNative - 未定义不是一个对象(评估 x),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37421826/

相关文章:

javascript - 用javascript编写SDK

python - 我在 python 中遇到 "no module named ad"错误

javascript - 如何删除 React Native 上 <Text> 标签上文本的连字符?

reactjs - 在 React Native 的非根文件中导入 firebase 会导致 Android 模拟器出错

python - Microsoft Visual Studio 2013 中没有名为 Flask 的模块

ruby - 判断祖先是Ruby中的类还是模块的方法?

haskell 在列表中加载模块

javascript - 如何在 React-Native 中使用时间来发出通知?

react-native - Jest 未能测试导入 FontAwesomeIcon 的组件

react-native - 如何滚动到 React Native ListView 的底部