【问题标题】:React Native How to pass textinput values from child component to parent componentReact Native 如何将文本输入值从子组件传递到父组件
【发布时间】:2021-06-10 09:12:06
【问题描述】:

我想做一个简单的文本输入操作。用户可以输入它的名字。

我创建了基于类的父类 (App.js) 和基于函数的 (InputName.js)

我将父类中的子组件称为

<InputName />

我将 InputName.js 创建为

 const InputName = () => {

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

}

我的问题是如何从父组件中获取子组件中的 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)} />  )}};

// 输入文件

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

【问题讨论】:

  • 您的问题令人困惑?你能用额外的代码sn-p详细说明吗?
  • 我编辑了。我希望它现在定义清楚。

标签: react-native react-props textinput


【解决方案1】:

只需从父类中的子组件传递您的方法。这是一个工作示例:

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;

在您的主课中,只需执行以下操作:

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);

【讨论】:

  • 应该是
  • @nogabemist 对不起我的错。我在输入答案时忘记了这一点。我已经更新了答案,您是否在主视图中创建了 onTextChange?
  • onChangeText = (text) => {this.setState({inputValue: text});};我主要有这个。
  • @nogabemist 它必须工作。这是这样做的方法。您可以添加主屏幕和组件的完整代码吗?生病调试,让你知道。
  • @nogabemist 我已经更新了工作答案。立即尝试
猜你喜欢
  • 1970-01-01
  • 2017-12-06
  • 2018-12-28
  • 1970-01-01
  • 1970-01-01
  • 2020-04-23
  • 2022-06-30
  • 2021-09-20
  • 1970-01-01
相关资源
最近更新 更多