• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

javascript - 自定义 React Native UI 组件 : Invariant Violation

[复制链接]
菜鸟教程小白 发表于 2022-12-12 16:42:46 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我按照文档构建了一个自定义的 React Native UI 组件。这个想法是它将是 Google Maps iOS API 的一个实现,但目前它只是为了显示标准的 Apple map 。

我使用命令 react-native new-library GoogleMapView 构建了一个新模块。这会将所有模块文件添加到 /Libraries/GoogleMapView/

我有一个名为 GoogleMapViewManager.h 的文件,其中包含:

#import "RCTViewManager.h"

@interface GoogleMapManager : RCTViewManager

@end

GoogleMapManager.m 文件包含:

#import <MapKit/MapKit.h>
#import "GoogleMapManager.h"

@implementation GoogleMapManager

RCT_EXPORT_MODULE()

- (UIView *)view
{
    return [[MKMapView alloc] init];
}

@end

这显然是根据文档显示 map 所需的最低要求。这确实构建并且应用程序启动得很好。

在 JavaScript 方面,我有一个名为 GoogleMapView.js 的文件,其中包含:

'use strict';

var React = require('react-native');
var { requireNativeComponent } = React;

class GoogleMapView extends React.Component {
    render() {
        return <GoogleMap {...this.props} />;
    }
}

GoogleMapView.propTypes = {
    //
};

var GoogleMap = requireNativeComponent('GoogleMap', GoogleMapView);

module.exports = GoogleMapView;

同样,这或多或少是从文档中删除的,所以我希望它能够工作,除非我在某个地方犯了错误。

当我尝试对这个模块做任何事情时,比如将它包含在这样的文件中:

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

然后在 JSX View 中使用它,有点像这样(这是来 self 的 Map.js 组件):

render: function() {
    return (
        <View style={styles.flexContainer}>
            <GoogleMap/>
        </View>
    );
}

我收到“不变违规”错误。我不太清楚这意味着什么,完整的堆栈跟踪是这样说的:

localhost:8081/index.ios.bundle
line: 2041
message: Invariant Violation: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined. Check the render method of `Map`.'
2015-10-17 22:06:05.289 [warn][tid:com.facebook.React.JavaScript] 'devtools socket closed'

由于无法解决这个问题,我在这里有什么问题吗?



Best Answer-推荐答案


JavaScript 要求不正确,我需要使用:

var GoogleMap = require('../Libraries/GoogleMapView/GoogleMapView.js');

而不是试图将它全部与 React 要求捆绑在一起:

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

关于javascript - 自定义 React Native UI 组件 : Invariant Violation,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33191658/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap