React Native Paper TextInput styles are not applying - react-native

I Have started learning React Native. By following the documentation of React Native Paper I want a outlined Input box. I am using my QR code to test the app on my android phone. It displays the simple input element not the styled one
my App.js
import { StatusBar } from 'expo-status-bar';
import React from 'react';
import { StyleSheet, Text, View } from 'react-
native';
import Contants from 'expo-constants'
import Home from "./screens/Home";
import CreateContact from "./screens/CreateContact";
export default function App() {
return (
<View style={styles.container}>
{/* <Home /> */}
<CreateContact />
<StatusBar style="auto" />
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#ebebeb',
marginTop: Contants.statusBarHeight,
},
});
My CreateContact.js
import React, { useState } from 'react';
import { StyleSheet, Text, View, TextInput, } from 'react-native';
const CreateContact = () => {
const [Name, setName] = useState("");
const [Phone, setPhone] = useState("");
const [Email, setEmail] = useState("");
const [Salary, setSalary] = useState("");
const [Picture, setPicture] = useState("");
const [Modal, setModal] = useState(false);
return (
<View style={styles.root}>
<View>
<TextInput
mode="outlined"
selectionColor="green"
label="Name"
placeholder="Name"
value={Name}
onChangeText={(text) => setName(text)}
/>
</View>
</View>
)
}
const styles = StyleSheet.create({
root: {
flex: 1, //take compelete height
}
})
export default CreateContact;
Output in Browser :
my app in mobile screen is same like how it is displaying browser

Related

Components not rendering on screen react native expo

There are four components on a screen and only three out of four are rendering. <popDown/> is the component that isn't rendering. When I save the specific component's file the components get rendered. Note: I am saving an already fully updated file. It is running with no errors
The Screen
import react from 'react';
import {View,ScrollView} from 'react-native';
import { SafeAreaView, } from 'react-navigation';
import { PrProvider } from '../appFunctions/PrContext';
import PopDown from '../Components/PRForm/popDown';
import RepsWeightTextInput from '../Components/PRForm/RepsWeightTextInput';
import NotesInput from '../Components/PRForm/NotesInput';
import SubmitPr from '../Components/PRForm/SubmitPr';
const PrEnteryScreen = ({}) => {
return(
<View style = {{height:'100%',width:'100%',backgroundColor:'#141212'}}>
<SafeAreaView style = {{alignItems:'center'}}>
{/**
* use context for pr information
*/}
<PrProvider>
<PopDown />
<RepsWeightTextInput/>
<NotesInput/>
<SubmitPr/>
</PrProvider>
</SafeAreaView>
</View>
);
};
export default PrEnteryScreen;
popdown component
import { useContext, useEffect, useState } from "react";
import { TouchableOpacity,StyleSheet,View,LayoutAnimation,UIManager,Platform,useFocusEffect } from "react-native";
import { Children } from "react/cjs/react.production.min";
import SelectExerciseButton, { setIsOpen } from "./SelectExerciseButton";
import SelectExerciseModal from "../../Screens/SelectExerciseModal";
import { EXERCISE_DATA } from "../../Screens/SelectExerciseModal";
import { PrContext, PrProvider } from "../../appFunctions/PrContext";
if (
Platform.OS === "android" &&
UIManager.setLayoutAnimationEnabledExperimental
) {
UIManager.setLayoutAnimationEnabledExperimental(true);
}
const PopDown = () => {
const value = useContext(PrContext)
const [isOpen,setIsOpen] = useState(false)
const [listHeight,setListHeight] = useState(0)
const [textName,setTextName] = useState(value.exercise);//eventually want to change text based on exercise state
const toggleOpen = () => {
setIsOpen(value => !value);
LayoutAnimation.configureNext(LayoutAnimation.Presets.spring);
setTextName(value.exercise);
console.log(value.exercise);
}
useEffect(() =>{
EXERCISE_DATA.forEach(()=>{
setListHeight(value => value+50)
})
},[isOpen])
return(
<>
<TouchableOpacity
onPress = {toggleOpen}
>
<SelectExerciseButton />
</TouchableOpacity>
<View>
<SelectExerciseModal style = {isOpen ? styles.show: styles.hidden} listHeight = {listHeight} name = {textName}/>
</View>
</>
)
}
const styles = StyleSheet.create({
hidden:{
height:0,
},
show:{ backgroundColor: '#9B9A9A', width: 200 }
});
export default PopDown;
What I tried
I changed the hidden style height to see if it would render the list that is supposed to pop out of the button. The list did render which means the <selectExerciseButton/> is the only thing not rendering in <popDown/>.
const styles = StyleSheet.create({
hidden:{
height:100,
},
show:{ backgroundColor: '#9B9A9A', width: 200 }
});
<TouchableOpacity
onPress = {toggleOpen}
>
{children}
</TouchableOpacity>
<View>
<SelectExerciseModal style = {isOpen ? styles.show: styles.hidden} listHeight = {listHeight} name = {textName}/>
</View>
<PrProvider>
<PopDown>
<SelectExerciseButton/>
</PopDown>
<RepsWeightTextInput/>
<NotesInput/>
<SubmitPr/>
</PrProvider>
SelectExerciseButton
import { useNavigation,useFocusEffect } from "#react-navigation/native";
import { SafeAreaView, StyleSheet, Text, TouchableOpacity, View,LayoutAnimation } from "react-native";
import { useContext, useState,useEffect } from "react";
import { PrContext } from "../../appFunctions/PrContext";
/**
*
* #returns button that navigates to the a modal to select an exercise
*/
if (
Platform.OS === "android" &&
UIManager.setLayoutAnimationEnabledExperimental
) {
UIManager.setLayoutAnimationEnabledExperimental(true);
}
const SelectExerciseButton = () => {
const value = useContext(PrContext)
return(
<>
<View style = {styles.background}>
<Text style = {styles.Text}> {value.exercise} </Text>
</View>
</>
);
};
styles = StyleSheet.create({
background:{
backgroundColor: '#9B9A9A',
width: 335,
borderRadius:41,
alignItems:'center',
marginHorizontal:25,
height:200,
justifyContent:'center'
},
Text:{
fontWeight:'bold',
fontSize:40,
color:'white',
alignItems:'center'
}
})
export default SelectExerciseButton;

I have a list of around 150 objects and when i map it only around 20 load | React Native

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

React Native : how to make a value counter into textinput use formik

I got stuck here ,
I needed to make a counter into the textinput ,
and I realize ,the counter has conflict with the handleChange.
I have no idea how to modify it ,could you please take a look at my code ?Thank you so much in advance !!
import { StyleSheet, View } from 'react-native'
import React from 'react'
import AppForm from '../components/AppForm'
import * as Yup from 'yup'
import AppFormFieldWithCount from '../components/AppFormFieldWithCount';
const validationSchema = Yup.object().shape({
userName: Yup.string().required("Username can't be empty").label("Username"),
});
const EditScreen = () => {
const handleSubmit = async (userInfo) => {
console.log("receive information",userInfo);
};
return (
<View style={styles.container}>
{/** <TextInputWithCount number={12} maxLength={12} multiline={false} placeholder={"用户名"}/> */}
<AppForm
initialValues={{userName:''}}
validationSchema ={validationSchema}
onSubmit = {handleSubmit}
>
<AppFormFieldWithCount
name = {'userName'}
number ={15}
placeholder={"Username"}
maxLength ={15}
multiline={false}
/>
</AppForm>
</View>
)
}
export default EditScreen
const styles = StyleSheet.create({
container:{
padding:30,
}
})
Below ,it is the formik TextInput :
I have added the setValue for counting the text ,but the textinput will need the handleChange for getting the textinput information .Therefore, it has the conflict here, only one of them works in onChangeText function ...
import { StyleSheet, Text, View,TextInput} from 'react-native'
import React,{useState} from 'react'
import TextInputWithCount from './TextInputWithCount'
import AppErrorMessage from './ErrorMessage'
import { useFormikContext } from 'formik';
import colors from '../config/colors';
const AppFormFieldWithCount = ({name,number,maxLength,multiline,placeholder,...otherProps}) => {
const {setFieldTouched,handleChange,errors,touched} = useFormikContext();
const[value,setValue] = useState('');
return (
<>
<View style={styles.container}>
<TextInput placeholder={placeholder}
style={{flex:1,fontSize:18}}
placeholder = {placeholder}
multiline={multiline}
maxLength={maxLength}
onBlur = {()=>setFieldTouched(name)}
onChangeText = {
handleChange(name)
}
{...otherProps}
/>
<Text>{value === "" ? "0" : value.length}/{number}</Text>
</View>
<AppErrorMessage error={errors[name]} visible={touched[name]} />
</>
)
}
export default AppFormFieldWithCount
const styles = StyleSheet.create({
container:{
flexDirection:'row',
borderRadius:5,
borderWidth:1,
borderColor:colors.medium,
alignItems:'center',
width:'100%',
paddingHorizontal:10,
}
})

react native header state to screen possible?

I have a header where is a input field. Is that possible to pass the value to my screen file ?
Header.js
import React, { useCallback } from 'react';
import { StatusBar } from 'expo-status-bar';
import { StyleSheet, Text, View, TouchableOpacity, Dimensions, Platform, TextInput } from 'react-native';
import { useNavigation } from '#react-navigation/native';
import { Feather, AntDesign } from '#expo/vector-icons';
import Constants from 'expo-constants';
const width = Dimensions.get('window').width;
const headerHeight = 50;
const headerMessages = (text) => {
const navigation = useNavigation();
const [search, setSearch] = React.useState('');
const handleChangeInput = React.useCallback((e) => text(e));
const handleGoBack = useCallback(() => {
navigation.goBack();
});
return (
<View style={styles.container}>
<View style={styles.topHeader}>
<StatusBar color="#333" />
<Text style={styles.headerTitle}>Messages (0)</Text>
</View>
<View style={styles.searchContainer}>
<TextInput value={search} style={styles.searchInput} onChangeText={handleChangeInput} placeholder='Search Field' />
</View>
</View>
)
};
})
export default headerMessages;
Message.js
import * as React from 'react';
import { StyleSheet, Text, View, Pressable } from 'react-native';
const Messages = (props) => {
console.log(props.text);
return (
<View style={s.container}>
<Text>Messages</Text>
</View>
)
};
............................................................................................................................................................
If you want the 'search' state value in Message.js file, in that case initialize search in Message.js file and pass the setSearch hook as a callback function in the Header.js file.
And in the Header.js, get the state as props and set the value in textinput. In this way you will get the search value in the message.js file.

React-Native while pressing customButton nothing happens but when i press return it works

While pressing RoundButton component nothing happens, but when I press press return it works,
Here is my custom Button component
Custom Button:
import React from 'react';
import { Text, View, StyleSheet, TouchableOpacity, } from 'react-native';
export const RoundedButton = ({
style = {},
textStyle = {},
size = 125,
...props
}) => {
return( <TouchableOpacity style={[styles(size).radius,style]}>
<Text style={[styles(size).text,textStyle]}>{props.title} </Text>
</TouchableOpacity>);
};
Calling from component:
import React, {useState} from 'react';
import { Text, View, StyleSheet,TouchableHighlight } from 'react-native';
import {TextInput} from "react-native-paper";
import {RoundedButton} from '../../components/RoundedButton'
export const Focus = ({addSubject}) => {
const [focusSubject, setFocusSubject] = useState(null);
const [tempItem, setTempItem] = useState(null);
return (
<View style={styles.container}>
<View style={styles.titleContainer}>
<Text>Want something?</Text>
<View styles={styles.inputContainer} >
<TextInput onSubmitEditing={({ nativeEvent }) => {
setTempItem(nativeEvent.text);
addSubject(nativeEvent.text)
}} />
<RoundedButton size={100} title="+" onPress={()=> {addSubject(tempItem)}} />
</View>
</View>
</View>
);
}
You need to call onPress on TouchableOpacity
Custom Button:
import React from 'react';
import { Text, View, StyleSheet, TouchableOpacity, } from 'react-native';
export const RoundedButton = ({
style = {},
textStyle = {},
size = 125,
...props
}) => {
return(
<TouchableOpacity onPress={props.onButtonHandler} style={[styles(size).radius,style]}>
<Text style={[styles(size).text,textStyle]}>{props.title} </Text>
</TouchableOpacity>
)};
In your component pass onButtonHandler
Calling from component:
import React, {useState} from 'react';
import { Text, View, StyleSheet,TouchableHighlight } from 'react-native';
import {TextInput} from "react-native-paper";
import {RoundedButton} from '../../components/RoundedButton'
export const Focus = ({addSubject}) => {
const [focusSubject, setFocusSubject] = useState(null);
const [tempItem, setTempItem] = useState(null);
return (
<View style={styles.container}>
<View style={styles.titleContainer}>
<Text>Want something?</Text>
<View styles={styles.inputContainer} >
<TextInput onSubmitEditing={({ nativeEvent }) => {
setTempItem(nativeEvent.text);
addSubject(nativeEvent.text)
}} />
<RoundedButton size={100} title="+" onButtonHandler={()=> {addSubject(tempItem)}} />
</View>
</View>
</View>
);
}