I'm trying to build a custom input component, and render it on another page
Here is my code
import React from "react";
import { View, TextInput, StyleSheet, Text } from "react-native";
import {
widthPercentageToDP as wp,
heightPercentageToDP as hp,
} from "react-native-responsive-screen";
import { MaterialCommunityIcons } from "#expo/vector-icons";
class RegisterTextBox extends React.Component {
render() {
const RegisterTextInput = ({
value,
placeholder,
onChangeText,
onBlur,
secureTextEntry,
inputStyle,
viewStyle,
eyeIcon = false,
}) => {
return (
<View style={[styles.container, viewStyle]}>
<TextInput
style={[styles.main, inputStyle]}
value={value}
onChangeText={onChangeText}
onBlur={onBlur}
placeholder={placeholder}
secureTextEntry={secureTextEntry}
/>
{eyeIcon ? (
<MaterialCommunityIcons
name="eye-off"
size={24}
color="black"
style={{ paddingTop: 5 }}
/>
) : (
<View />
)}
</View>
);
};
const styles = StyleSheet.create({
container: {
height: hp(5.4),
width: wp(65.2),
borderBottomColor: "grey",
borderBottomWidth: 1,
flexDirection: "row",
justifyContent: "space-between",
},
main: {},
});
}
}
export default RegisterTextBox;
I want const RegisterTextInput to be rendered and use dynamic data, but I seem to get an error when trying to use it "Error: RegisterTextBox(...): Nothing was returned from render.
this is how I'm trying to use it:
...
import RegisterTextInput from "../components/registerInput";
<View style={styles.emailInputsContainer}>
<RegisterTextInput
style={styles.emailInput}
placeholder="Email"
value={"email"}
onChangeText={}
/>
</View>
...
Where Is my problem?
Note: I want to use states on this component
Try with this...
import React from "react";
import { View, TextInput, StyleSheet, Text } from "react-native";
import {
widthPercentageToDP as wp,
heightPercentageToDP as hp,
} from "react-native-responsive-screen";
import { MaterialCommunityIcons } from "#expo/vector-icons";
class RegisterTextBox extends React.Component {
constructor(props) {
super(props);
}
render() {
const {
value,
placeholder,
onChangeText,
onBlur,
secureTextEntry,
inputStyle,
viewStyle,
eyeIcon = false,
} = this.props;
return (
<View style={[styles.container, viewStyle]}>
<TextInput
style={[styles.main, inputStyle]}
value={value}
onChangeText={onChangeText}
onBlur={onBlur}
placeholder={placeholder}
secureTextEntry={secureTextEntry}
/>
{eyeIcon ? (
<MaterialCommunityIcons
name="eye-off"
size={24}
color="black"
style={{ paddingTop: 5 }}
/>
) : (
<View />
)}
</View>
);
}
}
const styles = StyleSheet.create({
container: {
height: hp(5.4),
width: wp(65.2),
borderBottomColor: "grey",
borderBottomWidth: 1,
flexDirection: "row",
justifyContent: "space-between",
},
main: {},
});
export default RegisterTextBox;
Related
I am just starting with React, I spent some days learning about it. I want to navigate my app normally before futher studying, I hope someone can correct my code. I am not achieving what I want.
I have this on my App.js
import { StatusBar } from 'expo-status-bar';
import { StyleSheet, Text, View } from 'react-native';
import { colors } from './src/utils/colors';
import { Telefone } from './src/telefone';
import { Menu } from './src/menu';
import { NavigationContainer } from '#react-navigation/native';
import { createBottomTabNavigator } from '#react-navigation/bottom-tabs';
import { Procurar } from './src/procurar';
import PassageiroFrm from './src/PassageiroFrm';
function HomeScreen() {
return (
<View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
<Text>Home!</Text>
</View>
);
}
function ProcurarScreen() {
return (
<Procurar></Procurar>
);
}
function PassageiroScreen(navigation) {
return (
<PassageiroFrm></PassageiroFrm>
);
}
function SettingsScreen() {
return (
<View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
<Text>Settings!</Text>
</View>
);
}
function LoginScreen() {
return (
<Telefone></Telefone>
);
}
const Tab = createBottomTabNavigator();
export default function App({navigation}) {
return (
<View style={styles.container}>
<NavigationContainer>
<Tab.Navigator>
<Tab.Screen name="login" component={LoginScreen} />
<Tab.Screen name="cadastro" component={PassageiroScreen} />
<Tab.Screen name="início" component={ProcurarScreen} />
<Tab.Screen name="corridas" component={SettingsScreen} />
</Tab.Navigator>
</NavigationContainer>
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: colors.azul,
},
});
I can navigate fine from this screen. But how about navigation on other screens.
This component doesn't navigate to "cadastro" when the button is clicked.
import React from 'react';
import {View, Text, StyleSheet, Image} from 'react-native';
import {TextInput} from 'react-native-paper';
import { colors } from './utils/colors';
import { Button } from 'react-native';
import PassageiroFrm from './PassageiroFrm';
import { Procurar } from './procurar';
import { Link } from 'react-router-dom';
import { useNavigation } from '#react-navigation/native';
let i = 0;
export const Telefone = ( {}) => (
<View style={StyleSheet.container}>
<View style={{ justifyContent :'center', alignItems: 'center'}} >
<Image source={ require("./imagens/placa.jpg")}></Image>
</View>
<TextInput label="Entre com o seu telefone"/>
<Button color="#F0C228"
style={{ fontColor:"red", buttonText: {
color: "black",
} }} onPress={() => regiao()} title="Entrar"> </Button>
<Text>Ainda não sou passageiro</Text>
<Button color="#F0C228" onPress={() => navigation.navigate('cadastro') } title="ser um passageiro"> </Button>
</View>
)
function ProcurarScreen() {
const navigation = useNavigation();
}
const styles = StyleSheet.create({
container: {
flex: 1,
},
text: {
color: colors.branco
}
})
I would try the following in your Telefone component:
import React from 'react';
import {View, Text, StyleSheet, Image} from 'react-native';
import {TextInput} from 'react-native-paper';
import {colors} from './utils/colors';
import {Button} from 'react-native';
import PassageiroFrm from './PassageiroFrm';
import {Procurar} from './procurar';
import {Link} from 'react-router-dom';
import {useNavigation} from '#react-navigation/native';
let i = 0;
export const Telefone = () => {
const navigation = useNavigation();
return (
<>
<View style={StyleSheet.container}>
<View style={{justifyContent: 'center', alignItems: 'center'}}>
<Image source={require('./imagens/placa.jpg')}></Image>
</View>
<TextInput label="Entre com o seu telefone" />
<Button
color="#F0C228"
style={{
fontColor: 'red',
buttonText: {
color: 'black',
},
}}
onPress={() => regiao()}
title="Entrar">
{' '}
</Button>
<Text>Ainda não sou passageiro</Text>
<Button color="#F0C228" onPress={() => navigation.navigate('cadastro')} title="ser um passageiro">
{' '}
</Button>
</View>
</>
);
};
const styles = StyleSheet.create({
container: {
flex: 1,
},
text: {
color: colors.branco,
},
});
So basically, I moved const navigation = useNavigation(); inside Telefone component.
Good luck!
Actually I changed this
<Tab.Screen name="login" component={LoginScreen} />
to:
<Tab.Screen name="cadastro" component={PassageiroFrm} /> //Component itself not a function
so the navigation was available on the component
export const Telefone = ({navigation}) => {
return (
<>
<View style={StyleSheet.container}>
<View style={{justifyContent: 'center', alignItems: 'center'}}>
<Image source={require('./imagens/taxi_placa.jpg')}></Image>
</View>
<TextInput label="Entre com o seu telefone" />
<Button
color="#F0C228"
style={{
fontColor: 'red',
buttonText: {
color: 'black',
},
}}
onPress={() => regiao()}
title="Entrar">
{' '}
</Button>
<Text>Ainda não sou passageiro</Text>
<Button color="#F0C228" onPress={() => navigation.navigate('cadastro')} title="ser um passageiro">
{' '}
</Button>
</View>
</>
);
};
const styles = StyleSheet.create({
container: {
flex: 1,
},
text: {
color: colors.branco,
},
});
I am trying to make a reusable text input component, but I want to grab the value that the user types to manipulate it on the App.js
TextInputComponent.js
import React, {useState} from 'react';
import { StyleSheet, View, TextInput } from 'react-native'
const TextInputComponent = (props) => {
const [text, onChangeText] = useState("")
return (
<View>
<TextInput style={styles.container} value={text} onChangeText={onChangeText} placeholder="Enter"/>
</View>
)
}
const styles = StyleSheet.create({
container: {
height: 50,
width: 200,
padding: 10,
margin: 10,
borderWidth: 1,
borderColor: "black",
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'center',
},
});
export default TextInputComponent
App.js
import { StatusBar } from 'expo-status-bar';
import React, {useState} from 'react';
import { Button, StyleSheet, Text, TextInput, View } from 'react-native';
import TextInputComponent from './src/components/TextInputComponent';
export default function App() {
return (
<View style={styles.container}>
<View style={{flexDirection: 'row' }}>
<Text>Test</Text>
<TextInputComponent/>
<Button title="A" onPress={()=> console.log(-------> text value from TextInputComponent <-----)}></Button>
</View>
<StatusBar style="auto" />
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'center',
},
});
In order to access the state value created on the TextInputComponent.js what do I need to do?
One thing you can do is to pass a custom method through to the component and then use it as the onChangeText method:
const TextInputComponent = ({ customMethod }) => {
const [text, onChangeText] = useState("")
return (
<View>
<TextInput style={styles.container} value={text} onChangeText={(txt) => customMethod(txt)} placeholder="Enter"/>
</View>
)
}
Then in App.js you have the method:
customMethod = (txt) => {
// do something with txt variable
}
and:
<TextInputComponent customMethod={customMethod} />
Might require some tweaking to get it to work (I didn't test it), but that's the general idea.
I am using react navigation to navigate between different screens.
below is my code:
import React from 'react';
import { StyleSheet, Text, View, Button, TouchableOpacity } from 'react-native';
import { NavigationContainer } from '#react-navigation/native';
import { createStackNavigator } from '#react-navigation/stack';
import Home from "./screen/Home";
function Main({ navigation }) {
return (
<View style={styles.container}>
<View style={{flex: 0}}>
<Text>Welcome</Text>
</View>
<View style={{flex: 0}}>
<TouchableOpacity style={styles.btn} onPress={() => navigation.navigate('Home')}>
<Text>Goto Home</Text>
</TouchableOpacity>
</View>
</View>
);
}
const MainNavigator = createStackNavigator();
export default class App extends React.Component {
render() {
return (
<NavigationContainer>
<MainNavigator.Navigator >
<MainNavigator.Screen name="Main" component={Main} />
<MainNavigator.Screen name="Home" component={Home} />
</MainNavigator.Navigator>
</NavigationContainer>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1, backgroundColor: '#44210D', alignItems: 'center', justifyContent: 'center',
},
text: {
color: "black", borderWidth: 2, borderColor: "green"
},
btn: {
alignItems: "center", backgroundColor: "#DDDDDD", padding: 10,margin: 10
},
});
You can see in the image that there is a white space at the top. I want to know how can I change the color of that space.
LoginScreen.js
import React, { Component } from 'react'
import { Text, View, StyleSheet, TouchableOpacity, TextInput } from 'react-native'
import { color } from 'react-native-reanimated'
import Button from 'react-native-elements'
import ValidationComponent from "react-native-form-validator"
import axios from 'axios'
import AsyncStorage from '#react-native-community/async-storage'
export default class LoginScreen extends ValidationComponent {
render() {
return (
<View style = {{
width:'100%',
height:'100%',
justifyContent: 'center',
alignSelf: 'center',
alignContent: 'center',
alignItems: 'center' }}>
<Text style={styles.TextLogin}>DEW WATER</Text>
<TextInput placeholder={'Enter the user Name'}
onChangeText={(value) => this.setState({username : value})}
style={{ height:42, width:'80%',
borderBottomWidth:1 }}/>
<TextInput placeholder={'Enter the user password'}
onChangeText={(value) => this.setState({password : value})}
style={{ height:42, width:'80%',
borderBottomWidth:1, marginTop: '5%'}}/>
<View style={{
marginTop:'10%',
width:'80%'}}>
<Button titleStyle={{fontSize: 22,}} buttonStyle={{borderRadius:50,width:100,backgroundColor:'#307cee',marginBottom:5 }}
onPress={this._onPressButton} color='white' title='LoginScreen'></Button>
<View style={styles.Signup}>
<Text style={[styles.textSignup, {color: 'gray'}]}>Don't have an account?</Text>
<TouchableOpacity
onPress = {() => {this.props.navigation.replace("RegisterScreen")}}>
<Text style={[styles.textSignup, {color:'#3465d9', marginLeft: 4}]}>Signup</Text>
</TouchableOpacity>
</View>
</View>
</View>
)
}
}
const styles =StyleSheet.create({
TextLogin: {
fontSize: 35,
marginBottom: 60,
opacity: 0.9,
color:'#5f9ea0'
},
Signup: {
marginTop: 25,
flexDirection: 'row',
justifyContent: 'center'
},
textSignup: {
textAlign: 'center'
}
})
App.js
import React from 'react'
import { NavigationContainer } from '#react-navigation/native'
import { createStackNavigator } from '#react-navigation/stack'
import LoginScreen from './screens/LoginScreen'
import RegisterScreen from './screens/RegisterScreen'
import Template from './screens/Page/Template'
const Stack = createStackNavigator();
function App() {
return(
<NavigationContainer>
<Stack.Navigator>
<Stack.Screen name = 'Login' component = {LoginScreen}/>
<Stack.Screen name = 'Register' component = {RegisterScreen}/>
<Stack.Screen name = 'Template' component = {Template}/>
</Stack.Navigator>
</NavigationContainer>
)
}
export default App;
Here is the code, I'm not able to invoke removeItem function while clicking on Icon tag.Please help me out I'm a beginner in react native.I'm stuck for 3 days.
Please help me out in proper way of calling function.Thanks in advanced
import React from 'react';
import { StyleSheet, Text, View,TextInput,KeyboardAvoidingView,Dimensions,ScrollView,Alert,TouchableOpacity,Button,TouchableHighlight } from 'react-native';
import Icon from 'react-native-vector-icons/Entypo';
var {height, width} = Dimensions.get('window');
var d = new Date();
export default class App extends React.Component {
constructor(props){
super(props);
this.state = {
noteList: [],
noteText: ''
}
}
addItems(){
var a = this.state.noteText;
this.state.noteList.push(a)
this.setState({
noteText:''
})
console.log(this.state.noteList)
}
removeItem(key) {
console.log('removeItem is working',key);
}
render() {
return (
<KeyboardAvoidingView style={styles.container} behavior="padding" enabled>
<View style={styles.header}>
<Text style={{fontSize: 20}}>NOTE APPLICATION</Text>
</View>
<View style={styles.body}>
<ScrollView>
{this.state.noteList.map(function(value,key){
return(
<View key={key} style={styles.bodyElements} >
<Text>{key}</Text>
<Text>{value}</Text>
<Text>{d.toDateString()}</Text>
<Icon onPress={(key) => this.removeItem(key)} name="cross" color="white" size={40}/>
</View>
)
})}
</ScrollView>
</View>
<View style={styles.footer}>
<TextInput style={{marginTop:10,marginLeft:10}}
placeholder="Jot down your thoughts before they vanish :)"
width={width/1.2}
underlineColorAndroid="transparent"
onChangeText={(noteText) => this.setState({noteText})}
value={this.state.noteText}
/>
<Icon style={{marginTop:15}} name="add-to-list" color="white" size={40} onPress={this.addItems.bind(this)}/>
</View>
</KeyboardAvoidingView>
);
}
}
I do not have your array data so i use a,b values. but mamy issue with map functionis here, you need to pass this as params . check in the code
import React from 'react';
import { StyleSheet, Text, View, TextInput, KeyboardAvoidingView, Dimensions, ScrollView, Alert, TouchableOpacity, Button, TouchableHighlight } from 'react-native';
// import Icon from 'react-native-vector-icons/Entypo';
var { height, width } = Dimensions.get('window');
var d = new Date();
export default class App extends React.Component {
constructor(props) {
super(props);
this.state = {
noteList: ['a','b'],
noteText: ''
}
}
addItems() {
var a = this.state.noteText;
this.state.noteList.push(a)
this.setState({
noteText: ''
})
console.log(this.state.noteList)
}
removeItem(key) {
console.warn('removeItem is working');
}
render() {
return (
<View >
<View style={styles.header}>
<Text style={{ fontSize: 20 }}>NOTE APPLICATION</Text>
<Button title="try" onPress={(key) => this.removeItem()} name="cross"
size={40} />
</View>
<View style={styles.body}>
{this.state.noteList.map(function(value,key){
return(
<View key={key} style={styles.bodyElements} >
<Text>{key}</Text>
<Text>{value}</Text>
<Text>{d.toDateString()}</Text>
<Button title="try"
onPress={() => this.removeItem()}
name="cross"
color="white"
size={40}/>
</View>
)
},this)}
</View>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#F5FCFF',
},
welcome: {
fontSize: 25,
textAlign: 'center',
margin: 10,
},
child: {
fontSize: 18,
textAlign: 'center',
margin: 10,
backgroundColor: 'green',
height: 100,
width: 200,
},
});