I've been reading the documentation and searching code here, but I can't find anything that helps me.
The idea is to change the email and password values when onChange is triggered so then it will be possible to retrieve that same data from App.js but I can't find any simple example of how this works using functional components.
After reading docs/inet seems like ReactNative has been changing a lot.
Here is my Form.js component:
import React, {useState} from 'react';
import {View, Text, TextInput} from 'react-native';
import styles from './styles';
const Form = () => {
const [email, setEmail] = useState('');
const [password, setPassword] = useState('');
return (
<View styles={styles.container}>
<Text style={styles.inputLabel}>Email</Text>
<TextInput
placeholder="Email"
onChangeText={email => setEmail(email)}
style={styles.input}
value={email}
/>
<Text style={styles.inputLabel}>Password</Text>
<TextInput
secureTextEntry={true}
placeholder="Password"
onChangeText={password => setPassword(password)}
style={styles.input}
value={password}
/>
</View>
);
};
export default Form;
And my App.js component
import React from 'react';
import {View, Button} from 'react-native';
import styles from './styles';
import Form from './Form';
import Greeting from './Greeting';
import Terms from './Terms';
const handleButton = data => {
console.log(data);
};
const App = () => {
return (
<View style={styles.container}>
<Greeting />
<Form />
<Terms />
<View style={{marginTop: 35}}>
<Button
onPress={() => handleButton(Form['email'])}
style={styles.confirmBtn}
title="Crear cuenta"
/>
</View>
</View>
);
};
export default App;
Since Form.js is the child component of the App.js you need to initialise the state in App.js and pass the setState function to Form.js like below
App.js
import React, {useState} from 'react';
import {View, Button} from 'react-native';
import styles from './styles';
import Form from './Form';
import Greeting from './Greeting';
import Terms from './Terms';
const App = () => {
const [email, setEmail] = useState('');
const [password, setPassword] = useState('');
const handleButton = data => {
console.log(email);
};
return (
<View style={styles.container}>
<Greeting />
<Form setEmail={(e)=>setEmail(e)} setPassword={(p)=>setPassword(p)} />
<Terms />
<View style={{marginTop: 35}}>
<Button
onPress={() => handleButton()}
style={styles.confirmBtn}
title="Crear cuenta"
/>
</View>
</View>
);
};
export default App;
Form.js
import React, {useState} from 'react';
import {View, Text, TextInput} from 'react-native';
import styles from './styles';
const Form = ({setEmail,setPassword}) => {
return (
<View styles={styles.container}>
<Text style={styles.inputLabel}>Email</Text>
<TextInput
placeholder="Email"
onChangeText={email => setEmail(email)}
style={styles.input}
value={email}
/>
<Text style={styles.inputLabel}>Password</Text>
<TextInput
secureTextEntry={true}
placeholder="Password"
onChangeText={password => setPassword(password)}
style={styles.input}
value={password}
/>
</View>
);
};
export default Form;
Related
So in summary I'm trying to pass my 'name' variable from my 'Signup.js' screen to my 'Home.js' by passing parameters to routes through my navigation.navigate() function.
So Here's my Signup.js Screen:
import firebaseConfig from '../firebase';
import {
Pressable,
Text,
TextInput,
View,
ActivityIndicator,
} from 'react-native';
import {useNavigation} from '#react-navigation/native';
import {initializeApp} from 'firebase/app';
import {
getAuth,
createUserWithEmailAndPassword,
onAuthStateChanged,
} from 'firebase/auth';
import React, {useState, useEffect} from 'react';
import styles from './Stylesheets/Signup-Stylesheet';
const Signup = () => {
const navigation = useNavigation();
// Initialize Firebase
const app = initializeApp(firebaseConfig);
const auth = getAuth(app);
const [name, setName] = useState(''); //THIS VARIABLE NEEDS TO GO TO HOME.JS
const [email, setEmail] = useState('');
const [password, setPassword] = useState('');
const [loading, setLoading] = useState(false);
function handleSignUpData() {
console.log(name);
setLoading(prevLoading => !prevLoading);
createUserWithEmailAndPassword(auth, email, password)
.then(userCredential => {
const user = userCredential.user;
})
.catch(error => alert(error.message));
}
useEffect(() => {
onAuthStateChanged(auth, user => {
if (user) {
navigation.navigate('Home', {name: name});
}
});
}, []);
return (
<View style={styles.container}>
<View style={styles.welcome}>
<Text style={styles.welcomeText}>
Hello! Tell me more about yourself:
</Text>
</View>
<View style={styles.form}>
<TextInput
onChangeText={text => setName(text)}
placeholderTextColor={'#3c2a21'}
placeholder="Name"
style={styles.input}
/>
<TextInput
onChangeText={text => setEmail(text)}
placeholderTextColor={'#3c2a21'}
placeholder="Email"
style={styles.input}
/>
<TextInput
onChangeText={text => setPassword(text)}
secureTextEntry
placeholderTextColor={'#3c2a21'}
placeholder="Password"
style={styles.input}
/>
<Pressable onPress={handleSignUpData} style={styles.submit}>
{loading ? (
<ActivityIndicator size="large" color="black" />
) : (
<Text style={styles.submitText}>Submit</Text>
)}
</Pressable>
</View>
</View>
);
};
export default Signup;
And here's my Home.js screen:
import {StyleSheet, Text, View, BackHandler, Alert} from 'react-native';
import React from 'react';
const Home = ({route}) => {
console.log(route.params.name);
At this point in time as long as it logs it to the console it's fine. But I keep getting this error:
cannot read property 'name' of undefined
I've gone through the react navigation docs and I'm still not sure what's wrong with my code. Maybe It's the useEffect() function? So I'd really like some help here!
I've tried to change the parameters to something like:
navigation.navigate('Home', {username: name}) but I still get the same error.
I'm new and need to use useState but how do I bind the button in a separate component and the Text in another component?
components 1
const Bottom = () => {
const [counter, setCounter] = useState(0);
function number() {
setCounter(counter + 1);
}
console.log(counter);
components 1
<View style={styles.seperator}>
<TouchableOpacity style={styles.button} onPress={number}>
<Text style={styles.buttonText}>Save</Text>
</TouchableOpacity>
</View>
components 2
<Text style={styles.number}>{props.counter}</Text>
My full code
import React, {useState, useEffect} from 'react';
import {View, Text, StyleSheet} from 'react-native';
import styles from './Header.styles';
const Header = props => {
return (
<View style={styles.container}>
<View style={styles.headContainer}>
<Text style={styles.title}>TODO..</Text>
<Text style={styles.number}>{props.counter}</Text>
</View>
</View>
);
};
export default Header;
import React, {useState} from 'react';
import {
View,
Text,
Stylesheet,
TouchableOpacity,
TextInput,
} from 'react-native';
import {Header} from 'react-native/Libraries/NewAppScreen';
import styles from './Bottom.styles';
const Bottom = () => {
const [counter, setCounter] = useState(0);
function number() {
setCounter(counter + 1);
}
console.log(counter);
return (
<View style={styles.bottomContainer}>
<TextInput
style={styles.input}
placeholder="Todo"
onChangeText={text => setText(text)}></TextInput>
<View style={styles.seperator}>
<TouchableOpacity style={styles.button} onPress={number}>
<Text style={styles.buttonText}>SAVE</Text>
</TouchableOpacity>
</View>
</View>
);
};
export default Bottom;
The code has 2 components, I want the number to increase when I press the button
i uploaded all my code sorry if it's a bad question i'm new
I tried to import props and pages
What i understand is that you try to achieve this
const component1 = ({state1,setState1})=>{return()}
const component2 = ({state2,setState2})=>{return()}
then in your screen
const HomeScreen = () =>{
const [yourState1,setYourState1]= useState(someValue1);
const [yourState2,setYourState2]= useState(someValue2);
return(
<View>
<Component1 state1={yourstate1} setState1={yourSetstate1} />
<Component2 state2={yourstate2} setState1={yourSetstate2} />
</View>
)
}
Component1 can be your button, Component2 can be your Text Component
This way you can manipulate your state.
I tried executing the code. But it shows me error as "Syntax Error. The type cast expression is expected to be wrapped with paranthesis." Do i have to cover useState using paranthesis?
import React, {useState} from 'react';
import {View, TextInput, SafeAreaView} from 'react-native';
import styles from './styles';
const DestinationSearch = (props) => {
const [fromText, setFromText]=useState(initalState:'');
const [destinationText, setTextDestinationText]=useState(initalState:'');
return (
<SafeAreaView>
<View>
<TextInput
value={fromText}
onChangeText={setFromText}
style={styles.textInput}
placeholder="From"
/>
<TextInput
value={destinationText}
onChangeText={setTextDestinationText}
style={styles.textInput}
placeholder="Where to?"
/>
</View>
</SafeAreaView>
);
};
export default DestinationSearch;
initalState:'' is not valid javascript so change these two lines
const [fromText, setFromText] = useState('');
const [destinationText, setTextDestinationText] = useState('');
import React, { useState } from 'react';
import { View, TextInput, SafeAreaView } from 'react-native';
const DestinationSearch = (props) => {
const [fromText, setFromText] = useState('');
const [destinationText, setTextDestinationText] = useState('');
return (
<SafeAreaView>
<View>
<TextInput
value={fromText}
onChangeText={setFromText}
placeholder="From"
/>
<TextInput
value={destinationText}
onChangeText={setTextDestinationText}
placeholder="Where to?"
/>
</View>
</SafeAreaView>
);
};
export default DestinationSearch;
Snack
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>
);
}
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.