如何从 TextInput 获取值并使用 Button React Native 进行设置?

编码Noob28

我想通过按钮将从 TextInput 获得的用户名分配给用户名变量,并将其输出到下面的文本中。这意味着,如果用户输入用户名,则不会发生任何事情,但是当单击按钮时,输入的用户名将显示在文本中。我怎样才能做到这一点?

    function Login({navigation}) {

      return (
         <View style= {globalStyles.containerInput}>
             <TextInput 
                 style= {globalStyles.input} 
                 maxLength={20} 
                 minLegth={10} 
                 placeholder= "Username"
             />
         </View>
         <View style= {globalStyles.containerButton}>
             <Pressable 
                onPress={}
             >
                <Text style={globalStyles.text}>confirm</Text>
             </Pressable>
         </View>
         </View style={globalStyles.textOutput}>
             <Text style={globalStyles.text}>{usernameValue}</Text>
         </View>
      );
    }
拉赫曼哈伦

您需要将该值保持在状态中,并且可以使用该状态变量。并且每当您单击该按钮时,您都可以将状态变量用于 userName 例如

function Login({navigation}) {
     const [user, setUser] = useState("");
     const [userName, setUserName] = useState("");   
     const onValueChange = (text) => setUser(text); 
     const onButtonHandler = () => setUserName(user);
      return (
         <View style= {globalStyles.containerInput}>
             <TextInput 
                 style= {globalStyles.input} 
                 maxLength={20} 
                 minLegth={10} 
                 placeholder= "Username"
                 value={user}
                 onChangeText={onValueChange}
             />
         </View>
         <View style= {globalStyles.containerButton}>
             <Pressable 
                onPress={onButtonHandler}
             >
                <Text style={globalStyles.text}>confirm</Text>
             </Pressable>
         </View>
         </View style={globalStyles.textOutput}>
             <Text style={globalStyles.text}>{userName}</Text>
         </View>
      );
    }

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何获取值TextInput并添加到React Native中的Flatlist

如何在React Native中在TextInput上设置焦点或光标

如何在react-native中设置Onpress On Textinput

如何在不使用值的情况下设置 TextInput 字段 - React Native

提交后如何获取React Native TextInput来保持焦点?

如何在React Native中获取textinput的文本?

如何使用React Native中的引用更改TextInput的值?

如何使用React Native检测TextInput中的退格字符

React Native:如何使用 textInput 内联切换开关?

如何从TextInput获取值

如何在组件安装后聚焦 TextInput(使用聚焦 TextInput 初始化)-React Native

在React Native中从AsyncStorage设置TextInput值

React Native:如何使<TextInput />从右到左开始?

React Native:如何在React Native中发送TextInput的值?

React Native在Redux中获取TextInput值

React Native:如何将<TextInput />的高度和宽度设置为<View />容器?

如何在 React Native 中设置键盘上方的 TextInput 字段

如何在 React Native 中将 JSON 响应数组的特定数组索引设置为 TextInput

清除React Native TextInput

如何在React Native中获取TextInput相对于其父级或屏幕的光标位置

使用本机语音输入 React Native 设置 TextInput 默认值

我如何通过在 react-native 上使用 Nativebase 来使用 TextInput 和 Dropdown

如何使用 React Native 在 TextInput 值中连接数字代码和字符串

如何使用react-native在textInput字段中自动采用'/'(斜杠)

React Native-TextInput-如何一起使用value和defaultValue

React Native:将TextInput值设置为00:00

在React-native中的TextInput上进行PanResponder

聚焦时如何摆脱React Native Paper TextInput的底部边框

如何在React Native中在TextInput上应用点击事件