Quantcast
Channel: Active questions tagged react-native+typescript - Stack Overflow
Viewing all articles
Browse latest Browse all 6581

react-native textInput onChangeText issue

$
0
0
import React, {useRef, useState} from 'react';import {KeyboardAvoidingView, Platform, ScrollView, TextInput} from 'react-native';import {SafeAreaView} from "react-native-safe-area-context";export const ProfileScreen: React.FC = () => {    const [userInfo, setUserInfo] = useState({        name: '',        lastName: '',    });    const scrollViewRef = useRef<ScrollView>(null);    const inputNameRef = useRef<TextInput>(null);    const inputLastNameRef = useRef<TextInput>(null);    ...    return (<SafeAreaView style={{flex: 1}}><KeyboardAvoidingView                behavior={(Platform.OS === 'iOS') ? 'padding' : 'height'}                style={{flex: 1}}><ScrollView                    keyboardShouldPersistTaps={'handled'}                    ref={scrollViewRef}                    showsHorizontalScrollIndicator={false}                    showsVerticalScrollIndicator={false}><TextInput                        ref={inputNameRef}                        placeholder={'Your Name'}                        onChangeText={(text) => {                            setUserInfo((prevState) => ({...prevState, name: text}));                        }}                        value={userInfo.name}                        onSubmitEditing={inputLastNameRef.current?.focus()}                    /><TextInput                        ref={inputLastNameRef}                        placeholder={'Your Last Name'}                        onChangeText={(text) => {                            setUserInfo((prevState) => ({...prevState, lastName: text}));                        }}                        value={userInfo.lastName}                    /></ScrollView></KeyboardAvoidingView></SafeAreaView>    );};    

When I tap on the TextInput, the keyboard opens. However, when I press a key (triggering a state update), it closes. What am I missing?

When I tap on the TextInput, the keyboard opens. However, when I press a key (triggering a state update), it closes. What am I missing?


Viewing all articles
Browse latest Browse all 6581

Latest Images

Trending Articles



Latest Images