import { Appearance, StyleSheet } from 'react-native';
import { MD3LightTheme as DefaultLight, MD3DarkTheme as DefaultDark, PaperProvider } from 'react-native-paper';
import Navigation from './StackNavigator';
import * as Theme from './themes';
export function message(message) {
alert(message);
}
The issue you're encountering stems from an attempt to call App.message, which isn't directly possible because App in your context refers to a default export of a functional component, not an object or class with a method named message.
Here's why it happens and how you can solve it:
Context
In JavaScript/React, when you export default a component, it doesn't automatically create a context where instance methods or properties can be attached or called.
Solution
To achieve the desired functionality, you'll need to ensure that the message function is accessible from anywhere in your application. Here are a few approaches to do that:
1. Use a Separate Utility Function
Define the message function in a separate file that can be imported wherever needed.
utils.js
javascript
export function showMessage(message) {
alert(message);
}
App.js
javascript
import { Appearance, StyleSheet } from 'react-native';
import { MD3LightTheme as DefaultLight, MD3DarkTheme as DefaultDark, PaperProvider } from 'react-native-paper';
import Navigation from './StackNavigator';
import * as Theme from './themes';
import { showMessage } from './utils';
export default function App() {
return (
<PaperProvider theme={Appearance.getColorScheme() == 'light' ? Theme.lightTheme : Theme.darkTheme}>
<Navigation />
</PaperProvider>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'center',
},
});