I’m trying to develop an app that allows the user to input the title of a song on the screen ‘PostScreen’ and then when a button is clicked, see the title that they inputted on the home screen, called ‘TabOneScreen’. The problem is that it keeps saying that it ‘Cannot read property of ‘song’ of undefined’
This is the code for the screen ‘PostScreen’:
import { StatusBar } from 'expo-status-bar';
import { Platform, StyleSheet, TextInput, SafeAreaView, Pressable, Button } from 'react-native';
import { useState } from 'react';
import EditScreenInfo from '../components/EditScreenInfo';
import { Text, View } from '../components/Themed';
import { useNavigation } from '@react-navigation/native';
export default function PostScreen({ navigation }: RootStackScreenProps<'Post'>) {
const [song, setSong] = useState('song')
const saveSong = () =>{
navigation.navigate('Root', {
song: song,
});
};
return (
<View style={styles.container}>
<TextInput
style={styles.input}
placeholder='Enter a Song'
onChangeText={(val) => setSong(val)}/>
<Text> The song you will post is: {song}</Text>
<Button
title='Post Song'
mode='contained'
onPress={saveSong} />
</View>
);
};
const styles = StyleSheet.create({
container: {
flex: 1,
alignItems: 'center',
justifyContent: 'center',
},
input: {
borderWidth: 1,
borderColor: '#777',
padding: 8,
margin: 10,
width: 200,
color: 'blue',
},
title: {
fontSize: 20,
fontWeight: 'bold',
},
separator: {
marginVertical: 30,
height: 1,
width: '80%',
},
});
This is code for the screen ‘TabOneScreen’:
import { StyleSheet, Pressable } from 'react-native';
import { useState } from 'react';
import EditScreenInfo from '../components/EditScreenInfo';
import { Text, View } from '../components/Themed';
import { RootTabScreenProps } from '../types';
import { useRoute } from '@react-navigation/native';
export default function TabOneScreen({ navigation }: RootTabScreenProps<'TabOne'>) {
const route = useRoute();
return (
<View style={styles.container}>
<Text style={styles.title}>Me-Lody</Text>
<View style={styles.separator} lightColor="#eee" darkColor="rgba(255,255,255,0.1)" />
<Text>Here is where you will find your friends' posts for the day</Text>
<View style={styles.serparator} lightColor='#eee' carkColor="rgba(255,255,255,0.1)" />
<Pressable onPress={() => navigation.replace('Post')}
style={styles.link}>
<Text> Click me to post a song!</Text></Pressable>
<Text style={{ fontSize: 20}}> The song you recently posted is: {route.params.song}</Text>
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
alignItems: 'center',
justifyContent: 'center',
},
title: {
fontSize: 20,
fontWeight: 'bold',
},
separator: {
marginVertical: 30,
height: 1,
width: '80%',
},
link: {
borderWidth: 1,
borderColor: '#777',
padding: 8,
margin: 10,
width: 200,
color: 'blue',
}
});
2
Answers
I will share my working version
This is the code for App.tsx (which contains the createStackNavigator)
Below is the code for types used in this project
Below is the code for PostScreen.tsx
Below is the code for TabOneScreen.tsx
Please Try this out.
here is the full code maybe this will help you!
App.js