I am just giving a console.log (data) and navigating to another screen.
but I'm having the following error:
Component Exception
undefined is not an object
what am I doing wrong?
NOTE: When I use, navigation.goBack() works.
code below;
the error persists
Can anyone help me??
CODE UPDATED
import React, { useRef } from 'react';
import { useNavigation } from '#react-navigation/native';
import { Form } from '#unform/mobile';
import {
KeyboardAvoidingView,
Platform,
ScrollView,
Image,
} from 'react-native';
import logo from '../../assets/logo.png';
import Input from '../../components/Input';
import Button from '../../components/Button';
import { Container, FormContainer, Title, TitleContainer } from './styles';
export default function SignUp() {
const formRef = useRef(null);
const navigation = useNavigation();
const handleSignUp = data => {
console.log(data);
navigation.navigate('SignUpPersonalData', { data });
};
return (
<>
<KeyboardAvoidingView
style={{ flex: 1 }}
behavior={Platform.OS === 'ios' && 'padding'}
enabled
>
<ScrollView keyboardShouldPersistTaps="handled">
<Container>
<Image source={logo} />
<TitleContainer>
<Title>Crie sua conta</Title>
</TitleContainer>
<Form ref={formRef} onSubmit={handleSignUp}>
<Input
autoCorrect={false}
autoCapitalize="none"
keyboardType="email-address"
name="email"
icon="mail"
placeholder="E-mail"
/>
<Input
secureTextEntry
returnKeyType="send"
name="password"
icon="lock"
placeholder="Senha"
/>
<Button onPress={() => formRef.current.submitForm()}>
Continuar
</Button>
</Form>
</Container>
</ScrollView>
</KeyboardAvoidingView>
</>
);
Screenshot error:
SX
don't use useCallback, try the following.
const handleSignUp = (data) =>{
console.log(data);
navigation.navigate('SignUpPersonalData',{data: data});
}
Related
Assume the fetch returns an object of 150 items, of this 150 items only 20 are rendered as components. I've tried FlatList, LargeList and have watched multiple youtube videos and can't seem to get it to work.
I run this on the Expo Dev app, and when I scroll to the bottom of the list, no more load. Only about 20 cards are rendered.
This is what the end of the screen looks like, I try scroll down more and no more load.
Screen shot when scrolling to the bottom:
Screen shot after scrolling to the bottom:
This is the code for my home screen:
import React, { useEffect, useState } from 'react';
import {StyleSheet, ScrollView, Alert, ActivityIndicator} from 'react-native';
import HeaderTabs from '../components/HeaderTabs';
import Screen from '../components/Screen'
import Categories from '../components/Categories'
import SearchBar from '../components/SearchBar'
import ServiceItem from "../components/ServiceItem";
import tailwind from 'tailwind-react-native-classnames';
import { localServices } from '../data/localServices';
import colors from '../configs/colors'
const YELP_API_KEY = "RF2K7bL57gPbve8oBSiX23GYdCVxIl-KedmS-lyEafEZKNIn6DgsN6j88JvHolhiT4LH-VxT2NvDwgzl9yCTW-5REbbu3Cl5vwqCNUtGhnzzScPinQciOvs6PVBtY3Yx";
const HomeScreen = () => {
const [serviceData, setServiceData] = useState(localServices)
const [city, setCity] = useState("San Francisco")
const [activeTab, setActiveTab] = useState("Delivery");
const [loading, setLoading] = useState(false)
const getServicesFromYelp = () => {
const yelpUrl = `https://api.yelp.com/v3/businesses/search?term=Home+Services&location=${city}`;
const apiOptions = {
headers: {
Authorization: `Bearer ${YELP_API_KEY}`,
},
};
setLoading(true)
return fetch(yelpUrl, apiOptions)
.then((res) => res.json())
.then((json) => {
setLoading(false)
if(json.error) return Alert.alert('Sorry', json.error.description);
setServiceData(
json?.businesses?.filter((business) =>
// business.transactions.includes(activeTab.toLowerCase())
business
// city.includes(business.location.city)
)
)
}
);
};
useEffect(() => {
getServicesFromYelp();
}, [city, activeTab]);
return (
<Screen style={tailwind`bg-white flex-1`}>
<HeaderTabs activeTab={activeTab} setActiveTab={setActiveTab} />
<SearchBar setCity={setCity} city={city} />
<ScrollView style={tailwind`flex-1`} showsVerticalScrollIndicator={false}>
<Categories />
{loading && <ActivityIndicator size="large" color={colors.primary} style={tailwind`mt-2 mb-6`} />}
<ServiceItem serviceData={serviceData} />
</ScrollView>
</Screen>
);
}
const styles = StyleSheet.create({})
export default HomeScreen;
This is the code for the individual components:
ServiceItem.js:
import React from 'react';
import { View } from 'react-native';
import ServiceItemCard from "./ServiceItemCard";
const ServiceItem = ({ serviceData }) => {
return (
<View>
{serviceData?.map((item, index) => (
<ServiceItemCard key={index} item={item} />
))}
</View>
);
}
export default ServiceItem;
ServiceItemCard.js:
import React, { useState } from 'react';
import { Image, Text, TouchableOpacity, View, FlatList } from 'react-native';
import tailwind from 'tailwind-react-native-classnames';
import { Entypo } from '#expo/vector-icons';
import { MaterialCommunityIcons } from '#expo/vector-icons';
const ServiceItemCard = ({ item }) => {
const [loved, setLoved] = useState(false)
return (
<View style={tailwind`mx-4 mb-4`}>
<Image
source={{ uri: item.image_url ? item.image_url : null}}
style={tailwind`w-full h-48 rounded-lg`}
/>
<TouchableOpacity style={tailwind`absolute top-2 right-2`} onPress={() => setLoved(e => !e)}>
<Entypo name={`${loved ? 'heart' : 'heart-outlined'}`} size={28} color="#fff" />
</TouchableOpacity>
<View style={tailwind`flex-row items-center mt-1`}>
<View style={tailwind`flex-grow`}>
<Text style={tailwind`font-bold text-lg`} numberOfLines={1}>{item.name}</Text>
<View style={tailwind`flex-row items-center`}>
<MaterialCommunityIcons name="clock-time-four" size={13} color="#06C167" />
<Text style={tailwind`text-xs text-gray-700`}> 20-30 • min • {item.price}</Text>
</View>
</View>
<View style={tailwind`w-8 h-8 justify-center items-center bg-gray-100 rounded-full`}>
<Text style={tailwind`text-gray-600 text-xs`}>{item.rating}</Text>
</View>
</View>
</View>
)
}
export default ServiceItemCard;
I tried using FlatList, LargeList and messing around with infinite scrolling
I have a TextInput in my code that doesn't clear itself after submit and i have no idea on why it does that or how to solve it. I've looked at other posts that has this kinda issue? but none works or i don't really know where to place the code to make it clear itself after submiting.
Code
import React, { useState } from 'react';
import {
StyleSheet,
Text,
View,
TextInput,
Button,
} from 'react-native';
export default function AddList({ submitHandler }) {
const [text, setText] = useState('');
const changeHandler = (val) => {
setText(val);
}
return(
<View style={styles.container}>
<View style={styles.wrapper}>
<TextInput
style={styles.input}
placeholder='text'
onChangeText={changeHandler}
/>
<Button onPress={() => submitHandler(text)} title='ADD' color='#333' />
</View>
</View>
);
}
Simply create a new function after useState as below:
const onSubmit = useCallback(() => {
if (submitHandler) submitHandler(text)
setText("")
}, [text])
and modify textinput and button as below:
<TextInput
style={styles.input}
placeholder='What Tododay?'
onChangeText={changeHandler}
value={text}
/>
<Button
onPress={onSubmit}
title='ADD TO LIST'
color='#333'
/>
Do not forget to import useCallback from react.
I hope it help you.
I installed Navigation to take some data from a page and send to another (hopefully I will).
But it doesn't work and I get
"We couldn't find a navigation object. Is your component inside a navigator?"
File Insert.js
import React, { useState } from "react";
import { createAppContainer } from "react-navigation";
import { createStackNavigator } from "react-navigation-stack";
import { useNavigation } from "#react-navigation/native";
import ViewData from "./ViewData";
const MainNavigator = createStackNavigator({
ViewDataPage: ViewData
});
...
const Insert = props => {
...
createAppContainer(MainNavigator);
const navigate = useNavigation();
return (
<TouchableWithoutFeedback
onPress={() => {
Keyboard.dismiss();
}}
>
<View style={styles.inputContainer}>
<Input
placeholder="Your Name"
onChangeText={text => setEnteredName(text)}
value={enteredName}
/>
...
<View>
<Button
title="Submit"
onPress={() => sendValues(enteredName, enteredSurname)}
/>
<Button
title="Click"
onPress={() =>navigate("ViewDataPage", { name: "Jane" })
}
/>
</View>
</View>
</TouchableWithoutFeedback>
I think I made a mess, I can't even get the page "ViewData".
I would like to change page sending some values in the hooks
Any idea where I mistook?
"#react-navigation/native" is for React Navigation 5. You can't use it with React Navigation 4.
I didn't do a whole a lot of work on react-native to being with but the formik and react-native-elements is not getting anywhere with my setup. I am not really sure what I am missing exactly. Basically, the form cannot be submitted. I have a reusable button and input components made out of react native elements. The form don't get submitted. Out of curiousity, I also tried RN's default button to submit the form but it also doesn't work. My sandbox is here.
My setup is pretty straight forward as you can see below. Any help would be great on what I am missing exactly:
FormInput.js:
import React, { Component } from "react";
import { View, StyleSheet } from "react-native";
import { Input } from "react-native-elements";
export default class FormInput extends Component {
render() {
let { name, ...rest } = this.props;
return (
<>
<View>
<Input name={name} {...rest} />
</View>
</>
);
}
}
FormButton.js
import React, { Component } from "react";
import { Button } from "react-native-elements";
export default class FormButton extends Component {
render() {
let { title, ...rest } = this.props;
return (
<>
<Button title={title} {...rest} onPress={() => this.props.onPress} />
</>
);
}
}
App.js:
import React, { Component, Fragment } from "react";
import { Alert, Button, Image, StyleSheet, Text, View } from "react-native";
import FormInput from "./components/forms/FormInput";
import FormButton from "./components/forms/FormButton";
import * as yup from "yup";
import { Formik } from "formik";
class App extends Component {
handleSubmit = values => {
console.log("submitted values ", values);
};
render() {
return (
<View style={styles.app}>
<Formik
initialValues={{
name: ""
}}
onSubmit={values => {
this.handleSubmit(values);
}}
validationSchema={yup.object().shape({
name: yup
.string()
.min(3)
.max(25)
.required()
})}
>
{({
values,
handleChange,
errors,
setFieldTouched,
touched,
isValid,
handleBlur,
handleSubmit
}) => (
<Fragment>
<FormInput
name="name"
onChangeText={handleChange("name")}
onBlur={handleBlur("name")}
autoFocus
/>
<Button
onPress={() => handleSubmit}
title="React Native Button"
/>
<FormButton
title="React native elements child button"
onPress={() => handleSubmit}
/>
</Fragment>
)}
</Formik>
</View>
);
}
}
export default App;
Can you post the error if exist? And did you make a method for submit button so that it can pass the value ?
In you App.js you're buttons onPress should be like below:
<Button
onPress={handleSubmit}
title="React Native Button"
/>
<FormButton
title="React native elements child button"
onPress={handleSubmit}
/>
Your onPress do not need to be an annonymous function.
i am using drawer from native base for my react native application. when u click the menu button the drawer not open up and i get this error ( _this._drawer.open ) is not a fucntion what is the isse here is my code
import React, { Component } from 'react';
import {
AppRegistry,View
} from 'react-native';
import {ScrollableTab,TabHeading, Drawer, Container,Content, Header,
Title, Button, Left, Right, Body, Icon ,Text,Tab, Tabs } from 'native-base';
import SecondStatus from './component/StatusComponent';
import HeaderComponent from './component/headerComponent';
import Sports from './component/Sports';
import MainPage from './component/MainPage';
import SideBar from './component/SideBar';
export default class Point extends Component {
closeDrawer = () => {
this.drawer.close()
};
openDrawer = () => {
alert('asasa click');
console.log('asad--');
this._drawer.open();
};
render() {
return (
<Container>
<Drawer
ref={(ref) => { this._drawer = ref; }}
content={<SideBar />}
onClose={() => this.closeDrawer()} >
<Header >
<Left>
<Button transparent onPress={this.openDrawer}>
<Icon name='arrow-back' />
</Button>
</Left>
<Body>
<Title>UrduPoint</Title>
</Body>
<Right>
<Button transparent onPress=
{this.openDrawer.bind(this)}>
<Icon name='menu' />
</Button>
</Right>
</Header>
</Drawer>
</Container>
);
}
}
AppRegistry.registerComponent('Point', () => Point);
here is my SideBar.js
import React, { Component } from 'react';
import {
Text,
View,
StyleSheet
} from 'react-native';
export default class SideBar extends Component{
render(){
return(
<View>
<Text>
asad
</Text>
</View>
)
};
}
ps. this drawer is same as in npm 'react-native-drawer'
According to the native base documentation, you should call:
this.drawer.root.open()
I have used react-native-drawer this npm thats work for me
Here is a very basic working example using native-base
import React, { Component } from 'react';
import {
Container,
Header,
Left,
Button,
Icon,
Body,
Title,
Right,
Content,
Drawer,
Text
} from 'native-base';
import {
StyleSheet,
View,
ScrollView
} from 'react-native';
class SideBar extends Component {
render() {
return (
<Container>
<Content
bounces={false}
style={{ flex: 1, backgroundColor: '#fff', top: -1 }}
>
<Button transparent>
<Text>Action</Text>
</Button>
</Content>
</Container>
);
}
}
export default class Core extends Component {
openDrawer() {
this._drawer._root.open();
}
closeDrawer() {
this._drawer._root.close();
}
render() {
return (
<Drawer
ref={(ref) => { this._drawer = ref; }}
content={<SideBar navigator={this._navigator} />}
onClose={() => this.closeDrawer()}
>
<Container>
<Header>
<Left>
<Button
transparent
onPress={() => this.openDrawer()}
>
<Icon name='menu' />
</Button>
</Left>
<Body>
<Title>TITLE</Title>
</Body>
<Right />
</Header>
<Content>
</Content>
</Container>
</Drawer>
);
}
}
Here is the sample example of NativeBase Drawer provided in its docs with a note saying You need to create your own SideBar component and import it.
Drawer Sample Code
import React, { Component } from 'react';
import { Drawer } from 'native-base';
import SideBar from './yourPathToSideBar';
export default class DrawerExample extends Component {
render() {
closeDrawer = () => {
this.drawer._root.close()
};
openDrawer = () => {
this.drawer._root.open()
};
return (
<Drawer
ref={(ref) => { this.drawer = ref; }}
content={<SideBar navigator={this.navigator} />}
onClose={() => this.closeDrawer()} >
// Main View
</Drawer>
);
}
}
Check for Sidebar Sample Code from NativeBase-KitchenSink
this._drawer._root.open()
is working for me