In React Native, I am using Custom Component of TextInput with forwardRef, input.current.focus() and input.current.clear() is working fine, but input.current.value is always undefined.
I have google around a lot, and tried many things, but issue still persist. Below is my code.
Here is custom component code:
const AuthInput = (
{label = 'Label', isSecure = false, error = null, onChangeText},
textRef,
) => {
console.log('AUTHINPUT called');
return (
<View style={styles.container}>
<Text style={styles.label}>{label}</Text>
<TextInput
ref={textRef}
style={styles.input}
secureTextEntry={isSecure}
onChangeText={onChangeText}
/>
{error && <Text style={styles.error}>{error}</Text>}
</View>
);
};
const forwaredInput = React.forwardRef(AuthInput);
export default forwaredInput;
I am calling that component and passing 'ref' like below (Parent Component):
const emailFieldRef = useRef(null);
const onLogin = () => {
console.log(emailFieldRef.current.value);
};
return (
<SafeAreaView style={styles.container}>
<View style={styles.form}>
<AuthInput
ref={emailFieldRef}
label={'Email'}
error={emailError}
// eslint-disable-next-line no-shadow
onChangeText={email => {
// setEmail(email);
}}
/>
<StretchedButton title={'Sign in'} onPress={() => onLogin()} />
</View>
</SafeAreaView>
);
};