Cannot call a class as a function in react-native - react-native

I am currently working on a react-native project using EXPO but there occurred an error I will share my error and code where should i change
https://i.stack.imgur.com/kCMGk.png
App.js
import React from 'react';
import MovieList from './components/list';
import Detail from './components/detail';
import { createAppContainer } from 'react-navigation';
import { createStackNavigator } from 'react-navigation-stack';
const AppNavigator = createStackNavigator({
MovieList: {screen: MovieList},
Detail: {screen: Detail},
})
const App = createAppContainer(AppNavigator);
export default App();
detail.js
import React from 'react';
import MovieList from './components/list';
import Detail from './components/detail';
import { createAppContainer } from 'react-navigation';
import { createStackNavigator } from 'react-navigation-stack';
const AppNavigator = createStackNavigator({
MovieList: {screen: MovieList},
Detail: {screen: Detail},
})
const App = createAppContainer(AppNavigator);
export default App();

you have written the same stuff in both of the files i.e App.js and details.js.
In react-native generally we defined all the screens in one files similar to what you have done in App.js file and then for all the screens which you have defined in App.js, you have to make classes for the same for MovieList and Detail screens respectively.
Your MovieList and Details screens should look like this....
import React, { Component } from "react";
import { View } from "react-native";
export default class MovieList extends Component {
constructor(props) {
super(props);
this.state = {
};
}
render() {
return (
<View style={{ flex: 1, backgroundColor: 'green' }}>
<Text> Hello world </Text>
</View>
);
}
}

Related

Error: The component for route 'Home' must be a React component

I keep getting this error. I already went through all the posts related to this error and tried everything to solve this error but still, I don't know why I am getting this error.
Here is the complete error statement:
Error: The component for route 'Home' must be a React component. For example:
import MyScreen from './MyScreen';
...
Home: MyScreen,
}
You can also use a navigator:
import MyNavigator from './MyNavigator';
...
Home: MyNavigator,
}
App.js :
import React from 'react';
import { StyleSheet, Text, View } from 'react-native';
import * as firebase from "firebase"
import { createAppContainer } from 'react-navigation';
import { createStackNavigator } from 'react-navigation-stack';
import HomeScreen from './screens/HomeScreen'
import SignupScreen from './screens/SignupScreen'
import SigninScreen from './screens/SigninScreen'
import LoadingScreen from './screens/LoadingScreen'
const MainNavigator = createStackNavigator(
{
Home: { screen: HomeScreen },
Loading: { screen: LoadingScreen },
SignIn: { screen: SigninScreen },
SignUp: { screen: SignupScreen }
},
{
//launcher screen
initialRouteName: "Loading"
}
);
//create app container
const AppContainer = createAppContainer(MainNavigator);
export default class App extends React.Component {
render() {
return <AppContainer />;
}
}
Here is HomeScreen.js
I have changed nothing here, it's just a bare minimum boilerplate code
import React from 'react';
import { StyleSheet, Text, View } from 'react-native';
export default class HomeScreen extends React.Component {
render()
{ return (
<View style={styles.container}>
<Text>Open up App.js to start working on your app!</Text>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'center',
},
});

How to controll some actions or state outside when using react-navigation?

If I navigate A screen to B screen by react-navigation.
It will generate a Header automatically.
So if I want to add some button deal with like control state or call api on the Header.
I have to add static navigationOptions like this
class B extends Component {
static navigationOptions = ({ navigation }) => ({
title: `${navigation.state.params.barTitle}`,
headerBackTitle: null,
headerTintColor: '#fff',
headerRight:
<TouchableOpacity
style={{ flexDirection: 'row', paddingRight: 20 }}
onPress={() => {
callApi();
}}
>
<Text>Call API</Text>
</TouchableOpacity>
});
// other code...
}
But it declare static navigationOptions under class B.
If I set all of my react-navigation settings on another folder:
navigator/index.js
import { createAppContainer, createSwitchNavigator } from 'react-navigation';
import { createBottomTabNavigator } from 'react-navigation-tabs';
import RootScreen from '../screens/RootScreen';
import LoginStack from './LoginStack';
import MainStack from './MainStack';
const RootStack = createSwitchNavigator(
{
RootScreen: RootScreen,
LoginStack: LoginStack,
MainStack: MainStack
},
{
initialRouteName: 'RootScreen',
}
)
const AppContainer = createAppContainer(RootStack);
export default AppContainer;
navigator/LoginStack.js
import { createStackNavigator } from 'react-navigation-stack';
import LoginScreen from '../screens/LoginScreen';
import A from '../screens/A';
import B from '../screens/B';
const LoginStack = createStackNavigator(
{
LoginScreen: { screen: LoginScreen },
A: { screen: A },
B: { screen: B }
},
{
initialRouteName: 'LoginScreen',
headerMode: 'none'
}
);
export default LoginStack;
I can't figure it out, how do I achieve that control the state or call api actions on LoginStack.js file ?
Do not consider customization my Header. If I want to use react-navigation Header. Is the only way I have to declare static navigationOptions under class B ?
Your question is not clear enough.
From which file you are trying to call API in LoginStack.js?
By control state, you mean Navigation State or Component State?
What I have understood is you want to call API in each route Screen as API is in the parent file.
You can pass screenProps to call api as shown below.(You can edit the render method to add custom Header)
import React from 'react';
import {View, Text, Button} from 'react-native';
import { createAppContainer, createSwitchNavigator } from 'react-navigation';
import { createStackNavigator } from 'react-navigation-stack';
// Login Stack
import ScreenA from './ScreenA';
import ScreenB from './ScreenB';
const LoginStackRoute = createStackNavigator(
{
ScreenA: ScreenA,
ScreenB: ScreenB
},
{
initialRouteName: 'ScreenA',
headerMode: 'none'
}
);
var LoginStackContainer = createAppContainer(LoginStackRoute);
export default class LoginStack extends React.Component{
alertTest(){
alert('hi')
}
render(){
return(
<>
<View style={{height:50,width:'100%', backgroundColor:'red'}}>
<Text>Custom Header</Text>
</View>
<LoginStackContainer screenProps={{alertTest: ()=>this.alertTest()}}/>
</>
)
}
}
ScreenA or ScreenB can access the API in route file as
import React from 'react';
import {View, Text, Button} from 'react-native';
export default class ScreenA extends React.Component{
render(){
return(
<View>
<Text>This is Screen A</Text>
<Button
title='alert'
onPress={()=>{this.props.screenProps.alertTest()}}
></Button>
</View>
)
}
}

Confusion with react-navigation with react-navigator 3

I am using react-navigation v3 for navigation in my react-native app. When I start app, It navigates to final navigation page i.e. Electonics
AppNavigator.js
import { createStackNavigator, createAppContainer } from 'react-navigation';
import Home from '../container/Home';
import BooksScreen from '../container/BooksScreen'
import ElectronicsScreen from '../container/ElectronicsScreen';
const AppNavigator = createStackNavigator({
Home:Home,
Book: BooksScreen,
Electronics: ElectronicsScreen
});
export default AppContainer = createAppContainer(AppNavigator);
Home.js
import React, { Component } from 'react';
import { View, Text, TouchableOpacity as Button } from 'react-native';
export default class Home extends Component {
render() {
return (
<View>
<Text> Home Page </Text>
<Button
title="Books"
onPress={this.props.navigation.navigate('Book')}
>
</Button>
<Button
title="Books"
onPress={this.props.navigation.navigate('Electronics')}
>
</Button>
</View>
);
}
}
App.js
import AppContainer from './src/Navigation/AppNavigator';
export default class App extends Component {
render() {
console.log(this.props)
return (
<AppContainer />
);
}
}
When I run the code, it runs directly to the ElectronisScreen.js. How can I make it stay at the Home?
Change your AppNavigator to below.
const AppNavigator = createStackNavigator({
Home: Home,
Book: BooksScreen,
Electronics: ElectronicsScreen
},
{
initialRouteName: 'Home',
});
you have to define your initialRouteName.

(0,y.StackNavigator) is not a function

I was trying to use the react-navigation StackNavigator, and backtracked my code multiple times but couldn't find the issue. I was trying to build a simple navigation menu using this function for ios.
I'm using expo. So I started this project by creating a new expo project through terminal with the command "create-react-native-app testExpo". As shown below I modified the App.js a bit then created a screens folder -> then created two screens: "HomeScreen" and "LoginScreen"
These are the pictures of my code and the error
https://i.ibb.co/fQ9fjf2/Screen-Shot-2019-01-10-at-9-02-54-AM.png
https://i.ibb.co/7Xtx6jM/Screen-Shot-2019-01-10-at-9-03-01-AM.png
https://i.ibb.co/C8xbBVY/Screen-Shot-2019-01-10-at-9-03-07-AM.png
//Error picture
https://i.ibb.co/chgwYHT/Simulator-Screen-Shot-i-Phone-X-2019-01-10-at-08-59-08.png
App.js
import React, {Component} from 'react';
import { StyleSheet, Text, View } from 'react-native';
import {StackNavigator} from 'react-navigation'
import LoginScreen from './screens/LoginScreen'
import HomeScreen from './screens/HomeScreen'
export default class App extends React.Component {
render() {
return (
<AppNavigator />
);
}
}
const AppNavigator = StackNavigator({
LoginScreen: {screen: LoginScreen},
HomeScreen: {screen: HomeScreen}
})
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'center',
},
});
HomeScreen.js
import React, { Component } from 'react';
import {Text,
View,
StyleSheet} from 'react-native';
class HomeScreen extends Component{
render() {
return(
<View>
<Text>This is the home screen</Text>
</View>
);
}
}
export default HomeScreen;
LoginScreen.js
import React, {Component} from 'react';
import {Text,
View,
StyleSheet} from 'react-native';
class LoginScreen extends Component{
render() {
return(
<View>
<Text>This is the login screen</Text>
</View>
);
}
}
export default LoginScreen;
I expect the app to launch with multiple screens to the applications.

Element type is invalid : expected a string or class/function but got:undefined ,check render method of Home

I am creating an app in ReactNative and i got this issue. Following are the files containing code
index.js
import React from 'react';
import {AppRegistry} from 'react-native';
import Home from 'MobileApp/src/users/Home';
AppRegistry.registerComponent('MobileApp', () => App);
Home.js
import React, {Component} from 'react';
import { View,Button} from 'react-native';
import Profile from './Profile';
class Home extends Component{
onPressProfile(){
navigate('Profile', { name: 'Profile' })
}
render() {
return(
<View style={styles.viewStyle}>
<View style={styles.buttonStyle}>
<Button
onPress={this.onPressProfile}
title="Profile"
/>
</View>
</View>
);
}
}
const styles= {
viewStyle: {
flex: 1,
justifyContent: 'center',
},
};
export default Home;
Error is in my Home.js. what is the mistake?Thanks in advance.
In your index.js file, you should be using your Home component as entry point of your app.
Index.js
import React from 'react';
import {AppRegistry} from 'react-native';
import Home from 'MobileApp/src/users/Home';
AppRegistry.registerComponent('MobileApp', () => Home);
In your actual code, App is undefined.
You may have error on this line:
navigate('Profile', { name: 'Profile' })
need to define navigate to access it. Assuming you are learning from official documentation
const { navigate } = this.props.navigation;
And import navigation would be like:
import {
StackNavigator,
} from 'react-navigation';
const App = StackNavigator({
Home: { screen: HomeScreen },
Profile: { screen: ProfileScreen },
});
For further information how navigation works, check documentation