I'm following ReactNative Native Module Guide to write the java class that can be used in JS side. The exported method is show
from class ToastModule
(exported as ToastAndroid
). The show
method is below:
public void show(String message, int duration) {
Toast.makeText(getReactApplicationContext(), message, duration).show();
}
All work as expected with the toast button appear when I invoke ToastAndroid.show from Button onPress handler.
import React, { Component } from 'react';
import {
AppRegistry,
StyleSheet,
Text,
View,
Button,
NativeModules,
} from 'react-native';
const ToastAndroid = NativeModules.ToastAndroid
export default class App extends Component {
handleBTNPressed(){
ToastAndroid.show('Awesome', ToastAndroid.SHORT);
}
render() {
return (
<View style={styles.container}>
<Text style={styles.welcome}>
Welcome to React Native!!
</Text>
<Button title='click me' onPress={()=>this.handleBTNPressed()}/>
</View>
);
}
}
However, when I further change the function name from
@ReactMethod
public void show(String message, int duration) {
Toast.makeText(getReactApplicationContext(), message, duration).show();
}
to
@ReactMethod
public void showAgain(String message, int duration) {
Toast.makeText(getReactApplicationContext(), message, duration).show();
}
I encounter the following error:"undefined is not a function"
this error shows again if I add a new exported method as following:
@ReactMethod
public void showAgain2(String message, int duration) {
String mes = "Hi " + message;
Toast.makeText(getReactApplicationContext(), message, duration).show();
}
Is there anyone know which step I goes incorrectly?
EDIT==========================
There might already be a ToastAndroid
in ReactNative, so I change the name to MyToastExample
. However, now the error become the following
Does anyone encounter the same issue?
react-native run-android
after you change the function name? – Denvershow
method and change the display message for the toast, it won't take effect. – CretinismToastAndroid
in ReactNative, so I change the name toMyToastExample
, now the error become "undefined is not an object"(evaluating 'MyToastExample.show') – Cretinism