I want to save the values from all input fields to getdata()
, but I am getting undefined value
export default function Signupfor(props) {
// const phoneInput = useRef < PhoneInput > null;
const [text, setTextname] = useState();
function getdata() {
console.log('dsd');
console.log(text);
}
const {userInfo, log} = props?.route?.params;
console.log(log.name);
return (
<View style={styles.prheight}>
<View style={styles.form}>
<Text style={styles.r}>One Last Step</Text>
<TextInput
style={styles.forminput}
label="Name"
value={userInfo.user.name}
onChangeText={text => setTextname(text)}
/>
<TextInput
style={styles.forminput}
label="Email"
value={userInfo.user.email}
onChangeText={text => setTextemail(text)}
/>
<TextInput
style={styles.forminput}
label="Whatsapp Number"
keyboardType="numeric"
value={userInfo.user.number}
onChangeText={text => setTextnumber(text)}
// value={this.state.myNumber}
maxLength={10} //setting limit of input
/>
</View>
<View style={styles.buttonw}>
<Button color="#7743DB" title="Lets Go" onPress={() => getdata()} />
</View>
</View>
);
}
Here, name
and email
should not be able to be edited. I want to pass the value from value={userInfo.user.name}
to the getdata()
4
Answers
You can use a library like https://react-hook-form.com to check an example with react native on video.
Or you can right it yourself, in the example below any time you need access to input values you can read it from
text
andnumber
You can do something like this!!
Same goes for
email
.Your original method doesn’t populate the state unless you edit the text input field, this is because your initialState doesn’t have a value to start with. so firing
getData()
is reading empty state if the fields havent been changed.onChangeText={text => setTextname(text)}
Only fire if you edit the textInput field.
Also I think you might be missing props, so first check if you are getting the correct data from
props
by logging it.Once you have confirmed the props are available.
Set the initialState for name to
userInfo.user.name
Then pass the state
name
to your TextInput and it should populate the value by reading from state.Make sure to create states for any additional values you wish to save.
you can use formik package for making form in react native
Installation
Usage