【问题标题】:expected a string (for built-in components) or a class/function (for composite components) but got undefined期望一个字符串(用于内置组件)或一个类/函数(用于复合组件)但未定义
【发布时间】:2020-11-17 20:32:26
【问题描述】:

我的代码原样,我不知道我的错误在哪里

警告:React.createElement:类型无效 - 需要一个字符串(对于内置组件)或一个类/函数(对于复合组件)但得到:%s.%s%s,未定义,您可能忘记了从定义组件的文件中导出组件,或者您可能混淆了默认导入和命名导入。

app.js

import React, { Component } from 'react';
import {
  Navigator,
} from 'react-native';
import Movies from './Movies';

const RouteMapper = (route, navigator) => {
  if (route.name === 'movies') {
    return <Movies navigator={navigator} />;
  }
};

export default class App extends Component {
  render() {
    return (
      <Navigator
        // Default to movies route
        initialRoute={{ name: 'movies' }}
        // Use FloatFromBottom transition between screens
        configureScene={(route, routeStack) => Navigator.SceneConfigs.FloatFromBottom}
        // Pass a route mapper functions
        renderScene={RouteMapper}
      />
    );
  }
}

电影.js

import React, { Component } from 'react';
import {
  ScrollView,
  Text,
  View
} from 'react-native';
import { movies } from './data';

export default class Movies extends Component {
  render() {
    return (
      <View>
        <ScrollView>
          {movies.map((movie, index) => <Text>{movie.title}</Text>)}
        </ScrollView>
      </View>
    );
  }
}

【问题讨论】:

  • “您可能忘记从定义组件的文件中导出组件,或者您可能混淆了默认导入和命名导入。”

标签: javascript reactjs react-native react-redux react-router


【解决方案1】:

您可能忘记返回 jsx 或 undefined 以外的值,这是 JavaScript 中每个函数的默认返回值。

根据我的猜测(因为我不知道其他组件中有什么),错误来自RouteMapper,它会在条件失败的情况下进行条件返回而没有默认返回。您可能希望返回 null 作为此组件的默认返回值。

如果这不能解决跟踪渲染的组件并检查它们是否都有一个不是隐式或显式的 return 语句undefined

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-01-29
    • 2019-08-06
    • 1970-01-01
    • 1970-01-01
    • 2020-09-04
    • 1970-01-01
    • 2016-09-05
    • 1970-01-01
    相关资源
    最近更新 更多