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';

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

2 Comments

I think it should not matter because namespace is different for ToastAndroid (react-native) where as here it's importing from custom module.
@L.Learner, can you please share android side code?
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.