How to render class from different JS? ERROR: expecting a string - react-native

When I run this I get an error saying: Element type is invalid: expected a string (built in components) or a class/function but got undefined, check the render method for LOGIN.
I just want to display login on main screen from other JS file
This is the LOGIN.JS <--- Where I call in the index
'use strict';
import React, { Component, Image} from 'react';
import {View, Text, StyleSheet} from 'react-native';
var Login = React.createClass({
render: function() {
return (
<View style={styles.container}>
<Image style={styles.logo}
source={require('image!lock')} />
<Text>HI</Text>
</View>
);
}
});
INDEX.IOS.JS <-- Where I am calling Login to show on page
import React, { Component, Image } from 'react';
import {
AppRegistry,
StyleSheet,
Text,
View
} from 'react-native';
var Login = require('./Login');
export default class AwesomeProject extends Component {
render() {
return (
<View style={styles.container} >
<Text style={styles.welcome}>
Welcome!
</Text>
<Text style={styles.logins}>
</Text>
<Login />
</View>
);
}
}

React render function should return a View element to render.
Change this
var Login = React.createClass({ render: function() { return ( HI ); } });
to
var Login = React.createClass({
render: function() {
return (
<View style={{flex:1}}>
<Text>HI</Text>
</View>
);
}
});

You can use the same syntax for the Login.js file as the index.ios.js and declare it with ES6 Class syntax, and export it.
Also FYI it appears you are importing Image from React rather than React Native in both files.
import React, { Component} from 'react';
import {View, Text, StyleSheet, Image} from 'react-native';
export default class Login extends Component {
render() {
return (
<View style={styles.container}>
<Image style={styles.logo}
source={require('image!lock')} />
<Text>HI</Text>
</View>
);
}
}

Related

React Native: TypeError: undefined is not an object (evaluating 'navigation.navigate')

An error is reported when I run. My code is roughly like this:
App.js :
import React, { Component } from 'react';
import {Text, View, TouchableOpacity} from 'react-native';
export default class App extends Component {
render() {
const {navigation}=this.props;
return (
<View style={styles.comCardView}>
<TouchableOpacity onPress={()=>{navigation.navigate('Jwxt')}}>
<View style={styles.funcView}>
<Image source={require('./resource/images/icon.png')} style={styles.funcIcon}/>
<Text style={styles.funcIconDesc}>Touch Here</Text>
</View>
</TouchableOpacity>
</View>
);
}
}
Jwxt.js :
import React, { Component } from "react";
import {Text, View} from 'react-native';
export default class Jwxt extends Component {
render(){
return (
<View>
<Text>This is JWXT Page</Text>
</View>
)
}
}
navigators/AppNavigators.js :
import {createStackNavigator} from 'react-navigation'
import App from '../App'
import Jwxt from '../page/Jwxt'
export const AppStackNavigator=createStackNavigator({
App:{
screen:App
},
Jwxt:{
screen:Jwxt
}
})
When I perform the operation, I get this error.
TypeError: undefined is not an object (evaluating 'navigation.navigate')
Please point out my mistakes and how to modify them. THX :)

how to get state from child component

I need to get the state of the number from the component Numberplus
And display in the App component
app component:
import React from "react";
import { Text, View } from "react-native";
import Numberplus from "./Number";
function App() {
return (
<View>
<Text>{/* How to get Numberplus component State here */ Number}</Text>
<Numberplus />
</View>
);
}
export default App;
Numberplus component:
import { Button, Text, View } from "react-native";
function Numberplus() {
let [Number, setNamber] = useState(0);
return (
<View>
{/*<Text>{Number}</Text>*/}
<Button
onPress={() => {
setNamber(++Number);
}}
title="Plus"
/>
</View>
);
}
export default Numberplus;
See more details and display the output result
You can solve this problem either using redux or holding the state in your parent component. I can not explain whole redux here but here is how you can manage it with state.
App.js
import React from "react";
import { Text, View } from "react-native";
import Numberplus from "./Number";
function App() {
let [Number, setNumber] = useState(0);
return (
<View>
<Text>{/* How to get Numberplus component State here */ Number}</Text>
<Numberplus number={Number} onPress={() => {
setNumber(++Number);
}} />
</View>
);
}
export default App;
NumberPlus.js
import { Button, Text, View } from "react-native";
function Numberplus() {
return (
<View>
{/*<Text>{this.props.number}</Text>*/}
<Button
onPress={this.props.onPress}
title="Plus"
/>
</View>
);
}
export default Numberplus;

How to navigation goback function works in React native page including Webview

I am working with Webview in React native.
Header and footer is not webview, and header includes navigation.goBack() button in it.
But it is not working even footer has navigation.navigate() button which is working.
Here is my code.
import 'react-native-gesture-handler';
import * as React from "react";
import { ImageBackground, StyleSheet, Text, View, Image, TextInput, TouchableOpacity } from "react-native";
import { AntDesign } from '#expo/vector-icons';
import { WebView } from 'react-native-webview';
import { height, width } from 'react-native-dimension';
import HeaderLink from './header_link';
import Footer from './footer';
const styles = StyleSheet.create({
container: {
flex: 1,
// flexDirection: "column"
},
});
class LinkPageScreen extends React.Component {
render() {
this.state = {
url: this.props.route.params.url,
title: this.props.route.params.title,
};
var linkURL = this.state.url;
return (
<View style={styles.container}>
<HeaderLink
image={false}
imageSource={{}}
left={
<TouchableOpacity onPress={ () => this.props.navigation.goBack(null) }>
....
</TouchableOpacity>
}
/>
<WebView
source={{uri: linkURL}}
style={{marginTop: height(12), marginBottom: height(11)}}
/>
<Footer navigation={this.props.navigation} />
</View>
)
}
}
export default LinkPageScreen
I changed navigation.goBack into navigation.navigate() but it was not working also in header.
I checked for same error on stackoverflow but webview.goback() function was there, and my problem is not like that. I am sure.
I found the solution. Even I don't know why.
I changed the code like below.
class LinkPageScreen extend React.component {
render() {
return (
<View>
<WebView .... />
<Header />
<Footer />
......
Unbelivably it works, i just changed the order of tags between webview and header.
Anyone can explain to me why this is happening?
Thanks.
You can try the code below:
import { useNavigation } from '#react-navigation/native';
const {goBack} = useNavigation();
const navigateBack = useCallback(() => {
goBack();
}, [goBack]);
.
.
.
<TouchableOpacity onPress={navigateBack}>
....
</TouchableOpacity>

How to get parameters in the reaction native

I want to pass parameters to a view. I followed some examples, but to no avail. How do I do it?
A part of my code:
Home:
import React from 'react';
import { MaterialIcons } from '#expo/vector-icons';
import { useNavigation } from '#react-navigation/native';
import { View, Text, Image, StyleSheet, ScrollView } from 'react-native';
import colors from '../../components/colors';
import Shoe from '../../components/home/shoe';
import Teste from '../teste';
import Detail from '../detail';
export default function Home(props) {
return(
<View style={styles.storeProducts_line}>
<Shoe
source={require('../../images/home/tenisAdidasFalconFeminino.jpg')}
title="Tênis Adidas Falcon Feminino"
price="200,00"
onClick={() => props.navigation.navigate('Detail', {
productId: 12,
productTitle: 'Tênis Adidas',
})}
/>
<Shoe
source={require('../../images/home/tenisAdidasYeezySalt.jpg')}
title="Tênis Adidas Yeezy Salt"
price="175,90"
onClick={() => navigation.navigate('Detail')}
/>
</View>
);
}
Detail:
import React from 'react';
import { View, Text, StyleSheet, ScrollView, Image } from 'react-native';
export default function Detail({props, navigation}) {
const productId = navigation.getParam('userId');
// const productId = props.navigation.getParam('userId');
return(
<View style={styles.container}>
<Image
/>
<Text>
Detail
</Text>
<Text>
{productId}
</Text>
</View>
);
}
All the examples I saw the structure of the view is different, do I have to change the structure to work? I'm new to react-native, bear with me. Thanks in advance for your attention.
You can also use route to access the passed data from parent component.
Example:
import React from 'react';
import { View, Text, StyleSheet, ScrollView, Image } from 'react-native';
export default function Detail({route, navigation}) {
const {productId} = route.params;
// const productId = props.navigation.getParam('userId');
return(
<View style={styles.container}>
<Image
/>
<Text>
Detail
</Text>
<Text>
{productId}
</Text>
</View>
);
}
For more please do read this doc: Passing parameters to routes

NavigatorIOS problems

Hi I'm having some trouble using the NavigatorIOS in my app, it gives me the error:'undefined is not an object(evaluating 'this.props.navigator.push').
I tried to see some examples but I can't see the error, can someone help me?
Here is the code:
import React, { Component, PropTypes } from 'react';
import Dimensions from 'Dimensions';
import Menu from './Menu'
import {
AppRegistry,
StyleSheet,
Image,
TouchableHighlight,
NavigatorIOS,
FadeInView,
Text,
View
} from 'react-native';
class Home extends React.Component {
constructor(props, context) {
super(props, context);
this.onForward = this.onForward.bind(this);
}
onForward(Menu){
this.props.navigator.push({
component: Menu,
title: "Menu",
navigationBarHidden: true,
});
}
render() {
return (
<View style={styles.container}>
<Image
style={styles.img}
source={require('./img/scrittaNera.png')}
onLoadStart={(e) => this.setState({loading: true})}
/>
<TouchableHighlight style={styles.button} onPress={() => this.onForward()}>
<Text style={styles.buttonText}>Get Inspired</Text>
</TouchableHighlight>
</View>
);
}
}
Thanks
You need to bind the function to your Component in this case this can be done as follows:
<TouchableHighlight
style={styles.button}
onPress=this.onForward.bind(this)>
....
</TouchableHighlight>
Also you need to make sure your main component is wrapped inside a <NavigatorIOS> component. Such as:
import React, { Component, PropTypes } from 'react';
import { NavigatorIOS, Text } from 'react-native';
export default class NavigatorIOSApp extends Component {
render() {
return (
<NavigatorIOS
initialRoute={{
component: MyScene,
title: 'My Initial Scene',
}}
style={{flex: 1}}
/>
);
}
}