Reputation: 79
So I'm a newbie to React Native so I started a Udemy course to learn it. The course is a bit old so I'm not sure if the code is outdated but point is I was trying to learn how to do routing to change the screen in a simple app but when I click the button, nothing happens. App.js:
import React from 'react';
import { StyleSheet, Text, View, Platform, Image, ImageBackground } from 'react-native';
import {Button} from 'native-base';
import { render } from 'react-dom';
import Landing from './src/Landing';
import Search from './src/search';
var myBackground = require('./assets/icons/landing.jpg');
export default class App extends React.Component {
state = {
currentScreen: "landing"
}
switchScreen = (currentScreen) => {
this.setState({currentScreen});
}
renderScreen = () =>{
if(this.state.currentScreen === "landing") {
return (
<Landing switchScreen={this.switchScreen }/>
)
}
else if(this.state.currentScreen === "search") {
return(
<Search/>
)
}
}
render() {
return (
<View style={styles.container}>
{this.renderScreen()}
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
marginTop: Platform.OS === "android" ? 50 : 0
},
});
The landing component:
import React from 'react';
import {View, Text, Image, StyleSheet, ImageBackground} from 'react-native';
import {Button} from 'native-base';
var myBackground = require('../assets/icons/landing.jpg');
class Landing extends React.Component {
render() {
return(
<View>
<ImageBackground style={ styles.imgBackground } source={myBackground}>
<View style={styles.viewStyle}>
<Text style={styles.titleStyle}>Welcome to PokeSearch</Text>
<Button block={true} style={styles.buttonStyle} onPress={()=>this.props.switchScreen("screen")}>
<Text style={styles.buttonText}>Start Searching for Pokemon!</Text>
</Button>
</View>
</ImageBackground>
</View>
)
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
marginTop: Platform.OS === "android" ? 50 : 0
},
imgBackground: {
width: '100%',
height: '100%',
flex: 1
},
viewStyle: {
flex: 1,
flexDirection: 'column',
justifyContent: "center",
alignItems: "center",
marginTop: 50,
},
titleStyle: {
fontSize: 30,
color: 'red',
alignItems: 'center',
},
buttonStyle: {
margin: 10
},
buttonText: {
color: 'red'
},
});
export default Landing;
And the search which is very simple:
import React from 'react';
import {View,Text} from 'react-native';
class Search extends React.Component {
render() {
return (
<View>
<Text>Search</Text>
</View>
)
}
}
export default Search;
So when i click the button it should change to the search page but nothing happens. Also i noticed the button doesnt do the flash animation it should when clicked. Does anyone have any suggestions on how to get this working please and thanks in advance!
Upvotes: 0
Views: 126
Reputation: 220
You can build navigation by following this link.
https://reactnavigation.org/docs/en/getting-started.html
A typical implementation of navigation is here.
https://github.com/apiko-dev/Perfi/tree/master/app/navigation
Upvotes: 1