iOS 13(simulator ) |
xcode13 |
react-navigation6.x |
react-native 0.68.2
Drawer Navigator works fine But an Error occurres when adding useLegacyImplementation.
Also,I can't touch off my sidebar by navigation.openDrawer
and If I add useLegacyImplementation={true} the screen while turns white.
This is src_Drawer
import React ,{Component}from 'react';
import { Button, View ,Text,StyleSheet} from 'react-native';
import { createDrawerNavigator } from '#react-navigation/drawer';
const Drawer=createDrawerNavigator()
//two function
function HomeScreen(prop){
return(
<View style={styles.container}>
<Text style={styles.text}>Home Screen</Text>
<Button title={"Open Drawer"} onPress={()=>prop.navigation.openDrawer()}/>
<Button title={"Toggle Drawer"} onPress={()=>prop.navigation.toggleDrawer()}/>
</View>
)
}
function NewsScreen(prop){
return(
<View style={styles.container}>
<Text style={styles.text}>News Screen</Text>
<Button title={"jump to Home"} onPress={()=>prop.navigation.navigate('Home')}/>
</View>
)
}
export default class index extends Component{
render(){
return(
<Drawer.Navigator >
<Drawer.Screen name="Home" component={HomeScreen} />
<Drawer.Screen name="News" component={NewsScreen} />
</Drawer.Navigator>
)
}
}
const styles=StyleSheet.create({
container:{
flex:1,
justifyContent:'center',
alignItems: 'center',
},
text:{
fontSize:40
}
})
app.js :
import 'react-native-gesture-handler';
import React, { Component } from 'react'
import { View, Text } from 'react-native';
import { NavigationContainer } from '#react-navigation/native';
import Index from './src_Drawer';
export default class App extends Component{
render(){
return(
<NavigationContainer>
<Index/>
</NavigationContainer>
)
}
}
I want to know how to fix it.
I resolve this problem!!
Error Reason:
have incorrect react-native-reanimated
First I do this according to official interpretation:
npm install react-native-reanimated
But it doesn’t work.
Also it Not warning me in debugger
Till I run it On my iPhone
This is how i resolve it:
link:https://docs.swmansion.com/react-native-reanimated/docs/fundamentals/installation/
1.First step is to install react-native-reanimated as a dependency in your project:
yarn add react-native-reanimated
2.Add Reanimated's Babel plugin to your babel.config.js:
module.exports = {
presets: [
...
],
plugins: [
...
'react-native-reanimated/plugin',
],
};
3.yarn start --reset-cache
Related
I am trying to learn react native but getting following error
ReferenceError: navigation is not defined
I am a beginner can you please guide me to solve this error.
I have also tried to change to functional component but getting same error.
I am using expo for learning purpose
import 'react-native-gesture-handler';
import React, { Component } from "react";
import { ActivityIndicator, StyleSheet, Text, View, Image, Button,ImageBackground } from "react-native";
import { NavigationContainer } from '#react-navigation/native';
import { createStackNavigator } from '#react-navigation/stack';
class HomeScreen extends Component {
render() {
return (
<View style = {styles.container}>
<Text>Hello HomeScreen</Text>
<Button
title="Go to Profile"
onPress={() => navigation.navigate('Profile')}
/>
</View>
);
}
}
class Profile extends Component {
render() {
return (
<View style = {styles.container}>
<Text>Hello profile</Text>
</View>
);
}
}
const Stack = createStackNavigator();
class App extends Component {
render() {
return (
<NavigationContainer>
<Stack.Navigator initialRouteName="HomeScreen">
<Stack.Screen name="Home" component={HomeScreen} />
<Stack.Screen name="Profle" component={Profile} />
</Stack.Navigator>
</NavigationContainer>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: "center",
alignItems : 'center'
},
});
export default App;
You are using a class based component so use this.props.navigation
<Button
title="Go to Profile"
onPress={() => this.props.navigation.navigate('Profile')}
/>
I am trying to develop a todolist app using react native, for which I coded the home screen. I had following some docs, but it gives me this error. Please help.
I have executed the commands in the following order.
expo install react-native-gesture-handler react-native-reanimated react-native-screens react-native-safe-area-context #react-native-community/masked-view
npm install #react-navigation/native
npm install #react-navigation/stack
This is my code.
import React from 'react';
import { StyleSheet, Text, View ,Button} from 'react-native';
import 'react-native-gesture-handler';
import { NavigationContainer } from '#react-navigation/native';
import { createStackNavigator} from '#react-navigation/stack';
class HomeScreen extends React.Component {
constructor(props){
super(props);
this.state={}
}
render(){
return(
<View>
<View style={{margin:50}}>
<Button title="New Task"></Button>
</View>
</View>
)
}
}
export default function App() {
return (
<NavigationContainer>
<Stack.Navigator>
<Stack.Screen name="Home" component={HomeScreen} />
</Stack.Navigator>
</NavigationContainer>
);
}
You are not declaring Stack in your program include this line in your program:
const Stack = createStackNavigator();
Add this line in your App function like this:
export default function App() {
const Stack = createStackNavigator();
return (
<NavigationContainer>
<Stack.Navigator>
<Stack.Screen name="Home" component={HomeScreen} />
</Stack.Navigator>
</NavigationContainer>
);
}
Hope this helps!
Add this line to the top of your code
import { createStackNavigator } from "#react-navigation/stack";
In case someone looking for without using class...
import * as React from 'react';
import { View, Text } from 'react-native';
import { NavigationContainer } from '#react-navigation/native';
import { createStackNavigator } from '#react-navigation/stack';
function HomeScreen() {
return (
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
<Text>Home Screen</Text>
</View>
);
}
const Stack = createStackNavigator();
function App() {
return (
<NavigationContainer>
<Stack.Navigator>
<Stack.Screen name="Home" component={HomeScreen} />
</Stack.Navigator>
</NavigationContainer>
);
}
export default App;
Try on expo, click here
write this instead if you are using react native:
import { createNativeStackNavigator } from '#react-navigation/native-stack';
&&
const Stack = createNativeStackNavigator();
I am trying to create new drawer navigation but getting this below error
Unable to resolve "react-native-screens" from "node_modules\#react-navigation\drawer\src\views\DrawerView.tsx"
Failed building JavaScript bundle.
but same code i tried in new empty project it worked with react-native-drawer in older version of "react-navigation": "^2.6.2" but the same not working in "react-navigation": "^4.0.10", it shows that react-native-drawer is removed and #react-native/drawer is latest so we need to use that but its not working kindly help me resolving this...Code is in below
import * as React from 'react';
import { View, Text } from 'react-native';
import { NavigationContainer } from '#react-navigation/native';
import { createDrawerNavigator } from '#react-navigation/drawer';
function Feed() {
return (
<View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
<Text>Feed Screen</Text>
</View>
);
}
function Article() {
return (
<View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
<Text>Article Screen</Text>
</View>
);
}
const Drawer = createDrawerNavigator();
function MyDrawer() {
return (
<Drawer.Navigator>
<Drawer.Screen name="Feed" component={Feed} />
<Drawer.Screen name="Article" component={Article} />
</Drawer.Navigator>
);
}
export default function DrawerNavigator() {
return (
<NavigationContainer>
<MyDrawer />
</NavigationContainer>
);
}
Yup! this is old question but I want to answer for this. However we know that react native packages varies based on version updates. Follow the below steps if your using (version)V5x I am sure it works.
npm install --save react-navigation-drawer (Install Packages)
import { createDrawerNavigator } from 'react-navigation-drawer'; (Import Navigator using 'react-navigation-drawer')
npm install #react-navigation/drawer
or
npm install #react-navigation/drawer
To use this drawer navigator, import it from #react-navigation/drawer:
import { createDrawerNavigator } from '#react-navigation/drawer';
Add react-native-reanimated/plugin in babel.config.js file
module.exports = {
presets: ['module:metro-react-native-babel-preset'],
Plugin: ['react-native-reanimated/plugin'],
};
I know this is not a right solution but anyway it worked for me...
instead of #react-navigation/drawer we can use
import { createDrawerNavigator,DrawerItems} from 'react-navigation-drawer';
this works and I have pasted my working code below
In ReactNative update they moved createDrawerNavigator and DrawerItems from react-navigation to react-navigation-drawer...
import React from 'react';
import { StyleSheet, Text, View, SafeAreaView, ScrollView,Dimensions,Image } from 'react-native';
import { createAppContainer} from 'react-navigation';
import {createStackNavigator} from 'react-navigation-stack';
import { createDrawerNavigator,DrawerItems} from 'react-navigation-drawer';
import Login from '../screens/Login';
import Home from '../screens/Home';
const CustomDrawComponent=(props) => (
<SafeAreaView style={{flex:1}}>
<View style={{paddingTop:45, backgroundColor:'green'}}>
<View style={{height:150,alignItems:'center', justifyContent:'center'}}>
<Image source={require('../assets/Logos/userPic.png')}
style={{height:120,width:120,borderRadius:60}} />
</View>
</View>
<ScrollView>
<DrawerItems {...props}/>
</ScrollView>
<Text style={{paddingBottom:100, paddingLeft:65}}>Vision Cultura V1.0</Text>
</SafeAreaView>
)
const screens = createDrawerNavigator({
Main: {
screen: createStackNavigator({
Home: {screen: Home},
Login: {screen: Login},
}, {initialRouteName: "Login"})
},
Home: Home,
Login: Login
},
{
contentComponent:CustomDrawComponent
});
const index = createAppContainer(screens);
export default index;
I get this error and it's driving me insane, I cannot even start a simple application on React Native. I am using the most basic example, with a fresh project and still throws this error.
I use react-navigation v3xx
Someone please help because I am losing my mind, thank you.
Here is the code I have:
import React, {Component} from 'react';
import {Platform, StyleSheet, Text, View, Button, TouchableHighlight} from 'react-native';
import { createAppContainer, createStackNavigator, StackActions, NavigationActions } from 'react-navigation'; // Version can be specified in package.json
class Home extends React.Component {
static navigationOptions = {
title: "Home",
}
render() {
return (
<View style={styles.container}>
<Text>Home Page</Text>
<Button onPress={() => this.props.navigation.navigate('About')} title="All about me" />
</View>
);
}
}
class AboutMeMe extends React.Component {
static navigationOptions = {
title: "All Me",
}
render() {
return (
<View style={styles.container}>
<Text>Home Page</Text>
<Button onPress={() => this.props.navigation.goBack()} title="<< Back" />
</View>
);
}
}
const AppScreens = createStackNavigator({
Home: Home,
About: AboutMeMe
})
const App = createAppContainer(AppScreens);
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
},
});
export default App;
Hellow
As react navigation now depends on gesture so you must install an additional library to go after you have installed the react-navigation library
Run this command inside your project from your terminal
npm install --save react-native-gesture-handler
Then run this one
react-native link react-native-gesture-handler
These instructions are well explained here
https://reactnavigation.org/docs/en/getting-started.html#installation
for new react navigation version
Best regards
npm install --save react-native-gesture-handler
react-native link react-native-gesture-handler
I have a custom header like this
import React from 'react';
import { View, TouchableOpacity, Text, StyleSheet, Image } from 'react-native';
import { Header, Left, Right, } from 'native-base';
import { Icon } from 'react-native-elements';
export default class MainHeader extends React.Component {
pressSearch() {
this.props.navigation.navigate('Login');
}
render () {
return(
<Header style={{paddingTop:25}}>
<Left style={{marginBottom:10}}>
<TouchableOpacity>
<View style={{flexDirection:'row'}}>
<Image
style={styles.stretch}
source={require('../images/logoforplay.png')}
/>
<Text style={styles.headerText} > Eventlinn </Text>
</View>
</TouchableOpacity>
</Left>
<Right>
<TouchableOpacity
style={styles.headerButton}
onPress={this.pressSearch.bind(this)}
>
<Icon
name={'search'}
/>
</TouchableOpacity>
<TouchableOpacity
style={styles.headerButton}
>
<Icon
name={'add-location'}
/>
</TouchableOpacity>
<TouchableOpacity
style={styles.headerButton}
>
<Icon
name={'notifications'}
/>
</TouchableOpacity>
</Right>
</Header>
)
}
}
const styles = StyleSheet.create ({
mainContainer: {
flex:1,
backgroundColor:'white'
},
cardContainer: {
flex:1
},
stretch: {
height:35,
width:35,
},
headerText: {
fontSize:24,
marginTop:3,
},
headerButton: {
marginBottom:10,
marginLeft:15
}
})
My problem navigation.navigate not working.My routers working well because i can navigate to 'login' with another screen.I read something about pure components but still dont understand how to solve this problem.What do you suggest ? By the way i am new to react native.
If your header isn't a route within your Router at the head of your app you will need to use withRouter from react-router
import {withNavigation} from 'react-navigation'
export default withNavigation(MainHeader)
Now you should be able to access this.props.navigation without directly passing it down as a prop from a parent component.
use import { withNavigation } from 'react-navigation'; at the top and then export default withNavigation(FilmItem); at the end.
See doc : https://reactnavigation.org/docs/en/connecting-navigation-prop.html