简体   繁体   English

React:如何获取自定义 TextInput 组件的值?

[英]React: How Do I get the value of a custom TextInput Component?

I have code for a custom TextInput component and I want to use this component for both a username and password value for a login screen, however, I don't know how to retrieve the value of the text input for a specific instance.我有自定义 TextInput 组件的代码,我想将此组件用于登录屏幕的用户名和密码值,但是,我不知道如何检索特定实例的文本输入值。

import React from 'react';
import { TextInput, StyleSheet, View, ImagePropTypes } from 'react-native';

const TextInputCustom = (props) => {
  const [value, onChangeText] = React.useState();

  return (
    <View style={styles.container}>
    <TextInput
      placeholder={props.name}
      style={styles.textInput}
      onChangeText={text => onChangeText(text)}
      value={value}
    />
    </View>
  );
}

After Importing I created in my login screen导入后我在登录屏幕中创建

<TextInputCustom name="Username"/>
<TextInputCustom name="Password"/>

How do I get the value so that I can assign it to a variable for each TextInputCustom instance?如何获取该值以便可以将其分配给每个 TextInputCustom 实例的变量?

You need to move the value and onChange to the parent level:您需要将valueonChange移动到父级别:

import { TextInput, StyleSheet, View, ImagePropTypes } from 'react-native';

const TextInputCustom = (props) => {
  const {value, onChange} = props;

  return (
    <View style={styles.container}>
    <TextInput
      placeholder={props.name}
      style={styles.textInput}
      onChangeText={text => onChange(text)}
      value={value}
    />
    </View>
  );
}

And then use it like this:然后像这样使用它:

<TextInputCustom name="Username" value={username} onChange={onUsernameChange} />
<TextInputCustom name="Password" value={password} onChange={onPasswordChange} />

This is the practice that is used in general for simple components.这是一般用于简单组件的做法。 You don't need to handle the value in the component level, but in the component that is using your custom component.您不需要在组件级别处理值,而是在使用自定义组件的组件中处理值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在react-native中获得NavigatorIOS的子组件TextInput的值 - How to get value of child component TextInput of NavigatorIOS in react-native 从 React Native 中的 TextInput 组件获取值 - Get value from a TextInput component in react native 如何在 react-native 中获取 TextInput 值 - How to get the TextInput value in react-native 如何从 React Img 组件中获取值 - How do I get the value from a React Img component 如何在 React Native 的 TextInput 组件中获取动态引用和焦点 - How to get dynamic ref and focus in TextInput component on React Native ReactJS:如何正确使用此textInput组件? - ReactJS: How I do I use this this textInput component correctly? REACT NATIVE:如何获取 TextInput 的值和 Picker 的值 - REACT NATIVE: How to get value of TextInput and value of Picker 提交后如何让 React Native TextInput 保持焦点? - How do I get a React Native TextInput to maintain focus after submit? 如何将 TextInput 的值存储到本地存储并在应用程序以本机反应启动时获取它们? - How can I store the value of TextInput to local storage and get them when the app starts in react native? 如何获取已选中(复选框)的值并在本机反应中自动完成 textInput - How can I get the value of checked (checkbox) and autocomplete a textInput in react native
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM