简体   繁体   English

React Native 如何将文本输入值从子组件传递到父组件

[英]React Native How to pass textinput values from child component to parent component

I want to make a simple textinput operation.我想做一个简单的文本输入操作。 User can input its name.用户可以输入其名称。

I created class based parent (App.js) and functional based (InputName.js)我创建了基于 class 的父级(App.js)和基于功能的(InputName.js)

I called child component in parent class as我将父 class 中的子组件称为

<InputName />

I created InputName.js as我将 InputName.js 创建为

 const InputName = () => {

const [text, setText] = React.useState(null);
return ( <TextInput 
autoCapitalize='none' 
onChangeText={(text) => setText(text)} 
style={styles.userTextInput} />)

} }

My question is How can I reach onChangeText value in child component from parent.我的问题是如何从父组件中获取子组件中的 onChangeText 值。

   //imports
   import EmailInput from '../components/EmailInput';
   

   class Login extends Component { 

        state = {
        inputValue: '',
    }
        constructor(props) {
        super(props);
    }

    onChangeText = (text) => {
    this.setState({inputValue: text.target.value});
    };

    render() {
 return (
  <EmailInput value={this.state.inputValue} onChangeText={(text) => this.onChangeText(text)} />  )}};

// Input File // 输入文件

    const EmailInput = (onChangeText, value) => {
    const [text, setText] = React.useState(null);
    return ( <TextInput 
    autoCapitalize='none'
    value={value} 
    onChangeText={onChangeText} 
    style={//styles} />)
  }
                               

just pass your method from child component in parent class.只需从父 class 中的子组件传递您的方法。 here is an working example:这是一个工作示例:

import React from 'react';
import {TextInput} from 'react-native';

class EmailInput extends React.Component {
  constructor(props) {
    super(props);
    this.state = {};
  }

  render() {
    const {onTextChange, inputValue} = this.props;
    return (
      <TextInput
        autoCapitalize="none"
        value={inputValue}
        onChangeText={onTextChange}
      />
    );
  }
}

export default EmailInput;

in your main class just do the following:在您的主要 class 中,只需执行以下操作:

import React from 'react';
import {
  SafeAreaView,
} from 'react-native';
import EmailInput from '../components/EmailInput';

class LandingScreen extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      locals: {},
      inputValue: '',
    };
  }

  onTextChange = text => {
    this.setState({inputValue: text.target.value});
  };
  render() {
    return (
      <SafeAreaView style={styles.container}>
             <EmailInput
          inputValue={this.state.inputValue}
          onChangeText={text => this.onTextChange(text)}
        />
      </SafeAreaView>
    );
  }
}

const styles = StyleSheet.create({

});

const mapStateToProps = state => {
  const apiLoading = state.common.apiLoading;
  return {
    apiLoading,
  };
};

export default connect(mapStateToProps, actions)(LandingScreen);

暂无
暂无

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

相关问题 在 React Native 中从子组件 TextInput 更新父组件状态 - Updating Parent component state from Child component TextInput in React Native 如何在本机反应中将道具从父组件传递到子组件? - How to pass props from parent component to child component in react native? 如何在“React Native 功能组件”中将“状态”从子级传递给父级? - How to pass 'State' from child to parent in 'React Native Functional Component'? 将 SetState 从父组件传递给 React Native 中的子组件 - Pass SetState from parent component to a child component in React Native 如何在React Native中从父级访问子级组件值? - How to access child component values from the parent in react native? React Native:如何将参数从子组件道具传递给父组件 - React Native: How to pass params to parent component from child component prop 如何将信息从子组件传递给父组件中的函数? (反应本机) - How to pass info from a child component to a function in the parent component? (REACT NATIVE) 如何将数据从子组件传递到父组件的 state 反应原生? - how to pass data from child component to state of parent component react native? 如何使用 React Native 中的 props 将数组从父组件传递到子组件? - How to pass an array from a parent component to child component using props in React Native? 如何从 React Native 中的子组件更新父组件中的 State? - How To Update State In Parent Component From Child Component In React Native?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM