I am trying to create a text form with typescript in React Native framework. I create a TextInput component to use for Email, Password input. Here are my code:
TextInput.tsx
interface TextInputGroup extends ComponentPropsWithoutRef<"input"> {
errorText: string;
description: string;
}
export default function TextInput({ errorText, description, ...props } : TextInputGroup) {
return (
<View style={styles.container}>
<Input
style={styles.input}
selectionColor={theme.colors.primary}
underlineColor="transparent"
mode="outlined"
{...props}
/>
</View>
)
}
const styles = StyleSheet.create({
input: {
backgroundColor: theme.colors.surface,
},
})
SignUp.tsx
export default function RegisterScreen({ navigation } : { navigation : any }) {
const [name, setName] = useState({ value: '', error: '' })
const [email, setEmail] = useState({ value: '', error: '' })
const [password, setPassword] = useState({ value: '', error: '' })
return (
<Background>
<TextInput
label="Name"
returnKeyType="next"
value={name.value}
onChangeText={(text) => setName({ value: text, error: '' })}
error={!!name.error}
errorText={name.error}
/>
</Background>
)
}
2
Answers
I've found the problem in my code: in SignUp.tsx, some attributes like "label" don't exist in TextInputGroup or input HTML tag (when I extend ComponentPropsWithoutRef<"input">) so I change to ComponentPropsWithoutRef, add some required attributes like description and also delete some non-exist attributes in SignUp.tsx. My fixed code:
TextInput.tsx
SignUp.tsx
Instead of using
Input
fromreact-native-paper
, useTextInput
fromreact-native
.