1
I have an Input component that is a Textinput Wrapper (React-Native). When I type the value in my Input component and then try to show the value of the variable to which the text should have been assigned, the variable appears with the value it was created, not changing.
// MyInput
import React from 'react';
import { TextInput, Text } from 'react-native';
import styles from './styles';
interface InputProps {
label: String,
placeholder: String,
onChangeText: Function,
defaultValue?: String,
value: String,
}
const Input: React.FC<InputProps> = ({ label }: InputProps, ...props) => {
return (
<>
<Text style={styles.label}>{label}</Text>
<TextInput { ...props } style={styles.input}/>
</>
);
}
export default Input;
And the screen where I call the component:
// login
import React, { useState } from 'react';
import Input from '../../components/Input';
import { View, ImageBackground, Text, Image, Alert, Button } from 'react-native';
import styles from './styles';
import BackgroundLogin from '../../assets/images/BackGroundBanner01.png';
interface UserProps {
email: String,
password: String,
}
function Login() {
const [user, setUser] = useState<UserProps> ({email: 'default', password: ''});
return (
<ImageBackground source={BackgroundLogin} style={styles.container}>
<Input value={user.email} onChangeText={(text: String) => setUser({...user, email: text})} label="Email"></Input>
<Button
onPress={() => Alert.alert(`${user.email}`)}
title="Testar"
/>
</ImageBackground>
);
}
export default Login;
When I enter "Hello" text in Input, and press the test button, the application returns me "default" which is the initial value that the variable was instantiated.
If I remove the Wrapper and leave Textinput direct, the code works smoothly.
Citation
If I do it this way
const Input: React.FC<InputProps> = ({ label, ...props }) => {
the Textinput component shows this error "No Overload Matches this call.". as shown in this image: https://ibb.co/BKZ2yFz– Denis Correia
Yes forgot to talk about it, this is a typescript error, you should take the props interface of the Textfield component and extend with its props interface that has the label
– greguintow