I have the following in my React Native app:
Hub.js
:
export default class Hub extends Component {
static navigationOptions = {
header: null
};
...
render() {
return (
<View style={{flex: 1}}>
<TouchableOpacity
onPress={() => {
this.props.navigation.navigate('Link', {
site: 'CB Practice Test 1',
});
}}
>
<Text>Practice Test #1</Text>
</TouchableOpacity>
<TouchableOpacity
onPress={() => {
this.props.navigation.navigate('Link', {
site: 'CB Practice Test 3',
});
}}>
<Text>Practice Test #3</Text>
</TouchableOpacity>
<TouchableOpacity
onPress={() => {
this.props.navigation.navigate('Link', {
site: 'CB Practice Test 5',
});
}}
>
<Text>Practice Test #5</Text>
</TouchableOpacity>
<TouchableOpacity
onPress={() => {
this.props.navigation.navigate('Link', {
site: 'CB Practice Test 6',
});
}}
>
<Text>Practice Test #6</Text>
</TouchableOpacity>
</View>
</View>
)
}
}
webviewsFolder/Links.js
:
export default function Links({ route }) {
const link = route.params.site ===
'CB Practice Test 1' ? 'https://collegereadiness.collegeboard.org/pdf/sat-practice-test-1.pdf' :
'CB Practice Test 3' ? 'https://collegereadiness.collegeboard.org/pdf/sat-practice-test-3.pdf' :
'CB Practice Test 5' ? 'https://collegereadiness.collegeboard.org/pdf/sat-practice-test-5.pdf' :
'CB Practice Test 6' ? 'https://collegereadiness.collegeboard.org/pdf/sat-practice-test-6.pdf' :
'https://www.kaggle.com/';
return <WebView source={{ uri: link }} />;
}
My problem is that only CB Practice Test 1
and CB Practice Test 3
return the pages that they should, but the buttons for CB Practice Test 5
& 6
will return the url for CB Practice Test 3
. I have no idea why this is happening, I think it may be a syntax error, but if so, I'm not sure what it is or how to solve it.
Solution
this.props.navigation.navigate('Link', { site: 'CB Practice Test 1', });
Or try using switch case instead of shorthand.