2

I am trying to develop native module in android for react native. Exactly followed the link at https://facebook.github.io/react-native/docs/native-modules-android.html#content

but it is giving me error

/ReactNativeJS: undefined is not an object (evaluating '_ToastAndroid2.default.show')

I have implemented ToastAndroid.js

'use strict';
/**
 * This exposes the native ToastAndroid module as a JS module. This has a
 * function 'show' which takes the following parameters:
 *
 * 1. String message: A string with the text to toast
 * 2. int duration: The duration of the toast. May be ToastAndroid.SHORT or
 *    ToastAndroid.LONG
 */
import { NativeModules } from 'react-native';
module.exports = NativeModules.ToastAndroid;

and then in other Jsfiles tried to import using

import ToastAndroid from './ToastAndroid';
L.Learner
  • 129
  • 1
  • 1
  • 14

2 Answers2

1

change the name of Module "ToastAndroid" because ToastAndroid module is already in react-native package.

Anuj Jindal
  • 1,711
  • 15
  • 24
0

You are importing wrong. module.exports as the name implies exports your module to one of many exports that the file ToastAndroid.js can have. It's called a named export.

The correct import will therefore be import {ToastAndroid} from './ToastAndroid';

If you want to use import ToastAndroid from './ToastAndroid';

You should write export default NativeModules.ToastAndroid;

See this related answer for more information.

Community
  • 1
  • 1