ReactNativeJS: undefined is not an object (evaluating 'NativeModules.ToastCustomAndroid.show')困扰了一天,求大神解答



  • js代码
    import React, { Component } from 'react';
    import {
    AppRegistry,
    StyleSheet,
    Text,
    View,
    TouchableHighlight,
    ToastCustomAndroid,
    } from 'react-native';

    var { NativeModules } = require('react-native');
    module.exports = NativeModules.ToastCustomAndroid;
    console.log("是否存在"+NativeModules.ToastCustomAndroid);

    class reactdemo extends Component {
    render() {
    return (
    <View>
    <Text style={styles.welcome}>
    自定义弹出Toast消息
    </Text>
    <CustomButton
    text="点击自定义Toast方法"
    onPress={()=>NativeModules.ToastCustomAndroid.show("我是ToastCustomAndroid弹出消息",NativeModules.ToastCustomAndroid.SHORT)}
    />
    </View>
    );
    }
    }
    class CustomButton extends React.Component {
    render() {
    return (
    <TouchableHighlight
    style={styles.button}
    underlayColor="#a5a5a5"
    onPress={this.props.onPress}>
    <Text style={styles.buttonText}>{this.props.text}</Text>
    </TouchableHighlight>
    );
    }
    }

    const styles = StyleSheet.create({
    welcome: {
    fontSize: 20,
    textAlign: 'center',
    margin: 10,
    },
    button: {
    margin:5,
    backgroundColor: 'white',
    padding: 15,
    borderBottomWidth: StyleSheet.hairlineWidth,
    borderBottomColor: '#cdcdcd',
    },
    buttonText:{
    fontSize: 20,
    textAlign: 'center',
    }
    });

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

    可以运行,但是点击按钮就报ReactNativeJS: undefined is not an object (evaluating 'NativeModules.ToastCustomAndroid.show')这个错



  • @威威的达达锅
    不太确定,这个问题出在哪里,但是看着很别扭,我也是菜鸟,所以仅限猜测,1:var { NativeModules } = require('react-native');
    module.exports = NativeModules.ToastCustomAndroid;单独建一个js文件,import进来试试,2,NativeModules去掉试试,3检查下原生代码试试



  • @威威的达达锅 说:

    ToastCustomAndroid 没有被成功引入。
    检查对应的导出代码