I want to add a splashscreen to my code before the HomeScreen comes.
This is my new Splashscreen.js file
import React from 'react';
import { StatusBar, View, Text, ActivityIndicator } from 'react-native';
import HomeScreen from './screens/HomeScreen.js';
export default class SplashScreen extends React.Component {
render() {
return(
<View style={{ flex: 1, justifyContent: 'center', alignItems: 'center', backgroundColor: '#34495e'}}>
<StatusBar backgroundColor="#2c3e50" barStyle="light-content" />
<Text style={{ color: 'white', fontSize: 18}}>WELCOME</Text>
<ActivityIndicator color={'white'} />
</View>,
);
}
}
This is my new App.js file
import React, {Component} from 'react';
import { Keyboard, Alert } from 'react-native';
import {createAppContainer, createStackNavigator} from 'react-navigation';
import HomeScreen from './screens/HomeScreen.js';
/** Importing navigator */
import AppDrawerNavigator from './drawerNavigator';
/** Redux **/
import { createStore } from 'redux';
import { Provider } from 'react-redux';
import SplashScreen from './screens/SplashScreen.js';
const initialState = {
username: null,
password: null,
};
const reducer = (state=initialState, action) => {
if(action.type) {
console.log('NEW',action.name);
}
return {state, username: action.name, password: action.pass};
}
const store = createStore(reducer);
class App3 extends React.Component {
componentWillMount() {
this.state = {
view: <SplashScreen />
};
setTimeout(() => {
this.setState({
view: <HomeScreen />
})
}, 2000)
}
render() {
return (
// this.state.view,
<Provider store = {store}>
<AppContainer />
</Provider>
);
}
}
export default App3;
const AppStackNavigator = createStackNavigator(
{
Home: {
screen: HomeScreen
},
Welcome: {
screen: AppDrawerNavigator
}
},
{
initialRouteName: 'Home',
headerMode: "none",
}
);
const AppContainer = createAppContainer(AppStackNavigator);
If you want to check my other screens and files you can check this link.
I want to add the splashscreen file I created to my existing code without affecting the exisiting functionality. I want to achieve the following things:
Open application
Show splashscreen for a sec.
Go to login screen if not logged in before otherwise go to Welcomescreen directly from splashscreen
I would recommend you to take a look at Spencer Cali medium article. It covers adding a splash screen for RN apps step by step. How to Add a Splash Screen to a React Native App (iOS and Android)
For react-native you do not need to create file for splash screen.
just follow bellow package for splash screen i suggest you that is very good library with lots of users and yes i'm always prefer this library for splash screen.
you just need to create image for the splash screen whatever you want to show upon user on initial.
react-native-splash-screen
you may find all detail here and follow it step by step it will help you.
it always help for me!
Splash screen and loading screen are different. The splash screen is shown when application is open from termination status.
After that, some applications show loading screens for processing background logics as like checking user signin, get datas and so on.
In you case, you looks to show loading screen for authentication flows.
Use this ways.
React Navigation Auth flows
This flow is as below.
Show loading screen and check the user auth
navigate to another screen according to auth result.
if (loggedIn) { this.props.navigation.navigate('SignUpScreen') }
else { this.props.navigation.navigate('HomeScreen') }
Happy!
And if you want to use splash screen, not loading screen, I recommend to use rn-toolbox.
Related
I know this question has been asked several times but nowhere i found the answer. So I thought I will elaborate my question. Everything works fine in development. But when I make the standalone APK the splash screen doesn't goes away. I even tried to hide it after the component is loaded. That also doesn't work. What could be the issue ?
import { SplashScreen } from 'expo';
componentDidMount() {
setTimeout(function(){
SplashScreen.hide();
},2000);
}
try it:
create a screen: SplashScreen.js
and write the code bellow:
import React, { Component } from 'react';
import { View, StatusBar, Image } from 'react-native';
import introImage from '../assets/IntroPin3.gif';
export default class Loading extends Component {
render() {
return (
<View style={{ flex: 1, justifyContent:'center', alignItems:'center', backgroundColor: '#FDFDFF' }}>
<StatusBar hidden />
<Image
style={{height:350, width: 350 }}
source={ introImage}
/>
</View>
);
}
}
and in your app.js
write it:
import React, { Component } from 'react';
import SplashScreen from './Components/SplashScreen.js';
import Intro from './Components/Menu.js';
export default class Index extends Component {
state = {
ready: false,
};
componentDidMount() {
setTimeout(() => {
this.setState({ ready: true });
}, 5530);
}
render() {
if (this.state.ready === false) {
return <SplashScreen />;
}
return <Intro />;
}
}
try it, if this help you let me know.
import SplashScreen from 'react-native-splash-screen'
export default class WelcomePage extends Component {
componentDidMount() {
// do stuff while splash screen is shown
// After having done stuff (such as async tasks) hide the splash screen
SplashScreen.hide();
}
}
try this one... also you can read the documentation:
https://github.com/crazycodeboy/react-native-splash-screen
I've analyzed the needs of my application and decided I can more efficiently develop it with mobile friendly components on the web and present it via a web view in an app.
I need permissions to access camera and gallery (to take photos / videos, and upload photos / videos).
Basically, a single-screen app with a webview presenting the site with mobile-friendly components and the above permissions.
This is my current app.js:
import React from 'react';
import { Button, Text, View, TouchableOpacity, StyleSheet } from 'react-native';
import Ionicons from 'react-native-vector-icons/Ionicons';
import {
createStackNavigator,
createBottomTabNavigator,
createAppContainer,
} from 'react-navigation';
//import createStackNavigator, createBottomTabNavigator, createAppContainer in our project
import HomeScreen from './pages/HomeScreen';
import { Constants, Location, Camera, Permissions } from 'expo';
const ProfileStack = createStackNavigator(
{
//Definition of Navigaton from home screen
HomeScreen: { screen: HomeScreen },
},
{
//For React Navigation 2.+ change defaultNavigationOptions->navigationOptions
defaultNavigationOptions: {
//Header customization of the perticular Screen
headerStyle: {
backgroundColor: 'orange',
},
headerTintColor: '#FFFFFF',
title: '',
//Header title
},
}
);
const App = createBottomTabNavigator(
{
HomeScreen: { screen: HomeScreen },
},
{
defaultNavigationOptions: ({ navigation }) => ({
tabBarIcon: ({ focused, horizontal, tintColor }) => {
const { routeName } = navigation.state;
let IconComponent = Ionicons;
let iconName;
if (routeName === 'HomeScreen') {
iconName = `ios-information-circle${focused ? '' : '-outline'}`;
}
return <IconComponent name={iconName} size={25} color={tintColor} />;
},
}),
tabBarOptions: {
activeTintColor: 'orange',
inactiveTintColor: 'gray',
},
}
);
export default createAppContainer(App);
and './pages/HomeScreen':
//This is an example code for Bottom Navigation//
import React, {Component} from 'react';
//import react in our code.
import { Text, View, TouchableOpacity, StyleSheet, WebView } from 'react-native';
//import all the basic component we have used
export default class ProfileScreen extends React.Component {
//Profile Screen to show from Open profile button
render() {
return (
<WebView
source={{uri: 'https://mobilesite'}}
style={{marginTop: 20}}
/>
);
}
}
So far, the site opens on a single screen as expected, but there's a bottom navigation bar present; I'd also like to preferably hide the top bar if possible too as I've accounted for that in a mobile-friendly header on the mobile site as well.
Also, via the mobile web codebase, I'm utilizing <input type="file" /> for uploading. Is this compatible with React Native Permissions?
You're using createBottomTabNavigator() and passing the resulting navigator to your createAppContainer() which will create a bottom tab navigator.
If you pass a StackNavigator (via createStackNavigator() which you use for ProfileStack) to the createAppContainer() it'll render the stack as opposed to the bottom tab navigator which is what you want.
To remove the header altogether you can use headerMode: 'none' in the navigationOptions param.
You can see a basic version of what you want here (sans the lack of a header) in the React Navigation docs.
how to connect three or more screen in react native for Android and using react navigator.
Your question is quite unclear and and I can only assume what you want to achieve.
To be sure that we are on the same page, make sure you have run this command npm install --save react-navigation inside your project root directory.
Now in your app.js file you have to import StackNavigator from React-Navigation.
import { StackNavigator } from 'react-navigation';
Then you should import your screen components which later can be added to StackNavigator. For the sake of this example I import random files with random names. You wanted three and more, so let's import four.
import HomeScreen from './screens/HomeScreen';
import ProfileScreen from './screens/ProfileScreen';
import NewsScreen from './screens/NewsScreen';
import SettingsScreen from './screens/SettingsScreen';
All these files are located in the screens folder which itself is located in the same folder as app.js file. Now let's create our StackNavigator. These next lines go inside the app.js.
const NavigationApp = StackNavigator({
Home: { screen: HomeScreen },
Profile: { screen: ProfileScreen },
News: { screen: NewsScreen },
Settings: { screen: SettingsScreen }
})
Now your app.js file should look like this.
import React, { Component } from 'react';
import { StackNavigator } from 'react-navigation';
import HomeScreen from './screens/HomeScreen';
import ProfileScreen from './screens/ProfileScreen';
import NewsScreen from './screens/NewsScreen';
import SettingsScreen from './screens/SettingsScreen';
const NavigationApp = StackNavigator({
Home: {
screen: HomeScreen,
navigationOptions: {
headerTitle: 'Home'
}
},
Profile: { screen: ProfileScreen},
News: { screen: NewsScreen},
Settings: { screen: SettingsScreen}
})
export default class App extends Component {
render() {
return (
<NavigationApp />
);
}
}
Just in case to be super certain that you would get the full experience of our epic journey let's create one example screen with button which would let us navigate to another screen. Let's say it's Home and we have a button which would let us navigate to Profile.
import React, { Component } from 'react';
import { View, TouchableOpacity, Text } from 'react-native';
export default class HomeScreen extends Component {
render() {
const { navigate } = this.props.navigate
return (
<View style={{flex: 1, alignItems: 'center', justifyContent: 'center'}}>
<TouchableOpacity style={{color: 'steelblue'}} onPress={() => navigate('Profile')}>
<Text style={{fontSize: 12, color: 'white', padding: '10'}}>
Go to Profile screen
</Text>
</TouchableOpacity>
</View>
);
}
}
I hope this helps you to move on with your project and I really recommend to dive into the documentations of the libraries you will be using in the future because in the long run it is very useful and helps you get better in many aspects.
I'm new to React Native and I just don't get how react-navigation works. I've already spend multiple work days and tried different variants, but I don't even get the examples from https://reactnavigation.org/docs/intro/ to work.
My project is created with create-react-native-app AwesomeProject, like it's described on the React Native Docs.
So I went back to my last working point (without routing).
This is my File Structure:
root
- src
- - screens
- - - LoginScreen.js
- - - HomeScreen.js
- App.js
In my App.js I just perform an easy call:
import LoginScreen from './src/screens/LoginScreen';
export default LoginScreen;
This is a simplified version of my LoginScreen.js:
import React from 'react';
import {
StyleSheet,
Text,
View,
AppRegistry,
Button,
ReactNative,
} from 'react-native';
/**
* Actual View
*/
class LoginScreen extends React.Component {
constructor(props) {
super(props);
this.state = {
// some values
}
}
render() {
return (
<View style={[styles.containerOne]}>/>
// Some Input fields
<Button
onPress={this.loginButtonOnPress.bind(this)}
title="Login"
style={[styles.loginButton]}
/>
</View>
);
}
loginButtonOnPress() {
// Validation with fetch
}
}
export default LoginScreen;
/**
* Style
*/
const styles = StyleSheet.create({
containerOne: {
marginTop: -160,
justifyContent: 'center',
flex: 1,
alignItems: 'center',
}
loginButton: {
color: 'blue',
marginTop: 10,
}
});
The LoginScreen.js should be the first displayed page. I want to load my HomeScreen.js file when loginbuttonOnPress get called. How am I supposed to do that? I've already found something like
const SimpleApp = StackNavigator({
Login: { screen: LoginScreen },
Home: { screen: HomeScreen },
});
But I don't get this to work, too.
I'm thankfull for every help I can get.
Press login button then go to HomeScreen.
render() {
return (
<View style={[styles.containerOne]}>/>
// Some Input fields
<Button
onPress={() => navigation.navigate('Home'))}
title="Login"
style={[styles.loginButton]}
/>
</View>
);
}
You can try it work
App.js
const SimpleApp = StackNavigator({
Login: { screen: LoginScreen },
Home: { screen: HomeScreen },
},{ swipeEnabled: false }
);
export default SimpleApp
Try this inside you function
loginButtonOnPress() {
this.props.navigation.navigate('Home')
}
How do i go to the next screen in react native when i clicked a button?
Here is my code
import React, { Component } from 'react';
import {
AppRegistry,
StyleSheet,
Text,
View,
Button,
} from 'react-native';
import Test from './android/components/Test'
export default class ReactTest extends Component {
render() {
return (
<View style={styles.container}>
<Text style={styles.welcome}>
Welcome click the button to register.
</Text>
<Button
style={styles.cancelButton}
onPress={this.editUser}
title="Register"
color="#343434"
accessibilityLabel="Register a User."/>
</View>
);
}
}
I am new to react-native. I am trying to navigate from one screen to another when the register button is pressed.
Write a function at the top before render method for editUser to direct where to navigate to.
editUser = () => {
this.props.navigation.navigate("Screen");
};
You can use any of the following api
React Native Router
NavigatorIOS
React Navigation
Navigator
But I recommend you to use React Navigation. Here is an example how to use React Navigation
import {
StackNavigator,
} from 'react-navigation';
const App = StackNavigator({
Home: { screen: HomeScreen },
Profile: { screen: ProfileScreen },
});
class HomeScreen extends React.Component {
static navigationOptions = {
title: 'Welcome',
};
render() {
const { navigate } = this.props.navigation;
return (
<Button
title="Go to Jane's profile"
onPress={() =>
navigate('Profile')
}
/>
);
}
}