Home-Maintenance / home-maintenance-app

0 stars 0 forks source link

BLOCKER - We need to learn to pass States and Contexts between app screens - DONE #34

Open JonnyCodeMaster opened 2 days ago

yaiqbal commented 2 days ago

Code to pass data between screens (data that doesn't need to be stored globally)

import * as React from 'react'; import { Button, View, Text } from 'react-native'; import { NavigationContainer } from '@react-navigation/native'; import { createNativeStackNavigator } from '@react-navigation/native-stack';

function HomeScreen({ navigation }) { return ( <View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}> <Button title="Go to Job location" onPress={() => { navigation.navigate('Details', { postcode : "M1 65" })}} /> ); }

function DetailsScreen({ route, navigation }) {

const {postcode} = route.params

return ( <View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>

  **<Text>
         Job location - Post code: {JSON.stringify(postcode)}
</Text>**

); }

const Stack = createNativeStackNavigator();

function App() { return (

); }

export default App;

/* pre-requisite packages & code -npm install @react-navigation/native -npm install react-native-screens react-native-safe-area-context -npm install @react-navigation/native-stack

-for IOS - npx pod-install ios -Edit MainActivity.kt or MainActivity.java file which is located under .../android/app/src/main/java// MainActivity.kt... class MainActivity: ReactActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(null) } } or if MainActivity.java... public class MainActivity extends ReactActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(null); } }