在焦点上对外部库中的本机文本字段组件做出反应



我有一个寄存器组件,其中包含来自外部库的电话输入组件 https://github.com/thegamenicorus/react-native-phone-input 问题是我想在PhoneInput中的TextField聚焦后立即执行一些代码。

在检查库的文档时。我看到了一个名为textComponent的属性。可以用于电话输入组件中存在的文本字段的焦点吗

import React from 'react';
import {Image, StyleSheet, Text, View} from 'react-native';
import PhoneInput from 'react-native-phone-input'
export default class Register extends React.Component {
    constructor(props){
        super(props);
        this.state = {"phone": ""};
        this.countriesList =  [
            {
                "name": "India (भारत)",
                "iso2": "in",
                "dialCode": "91",
                "priority": 0,
                "areaCodes": null
            }];
        this.phoneTextField=null;
    }
    componentDidMount() {
        console.log('GrandChild did mount.');
        // console.log(this.phoneTextField);
    }
    render() {
        console.log(this.phoneTextField);
        console.log("Register Screen");
        return (
            <View style={{borderColor:'red', borderWidth:0, height:'100%' }}>
                <View style={{borderColor:'green', borderWidth:0, height:'20%' }}>
                    <View style={{borderColor:'blue', borderWidth:0, resizeMode:'contain', width:'50%', height:'100%'}}>
                        <Image
                            style={{width: '100%', height: '100%' }}
                            source={ require("../assets/logo.png") }
                        />
                    </View>
                </View>
                <View style={{borderColor:'green', borderWidth:0, height:'40%', marginLeft:'1%', marginTop:'5%' }}>
                    <View style={{borderColor:'blue', borderWidth:0, resizeMode:'contain', width:'100%', height:'100%'}}>
                        <Image
                            style={{width: '100%', height: '100%' }}
                            source={ require("../assets/welcomescreen.jpg") }
                        />
                    </View>
                </View>
                <View style={{ margin : '7%'}}>
                    <Text style={{ fontSize:20, fontWeight:'bold' }}>Save your time</Text>
                    <Text style={{ fontSize:14, color:'grey', paddingTop:'5%' }}>Please Enter your mobile number to proceed</Text>
                    <PhoneInput ref={ref => {
                            this.phoneTextField = ref;
                    }}
                                style={{paddingTop:'10%'}}
                                countriesList={this.countriesList}
                                initialCountry='in'
                                onChangePhoneNumber={(phone)=>{
                                    console.log(phone);
                                }}
                    />
                </View>
            </View>

        );
    }
}
const styles = StyleSheet.create({
    container: {
        flex: 1,
        backgroundColor: '#fff',
        alignItems: 'center',
        justifyContent: 'center',
    },
});

我的应用.js

import React from 'react';
import { StyleSheet, Text, View } from 'react-native';
import Register from "./screens/Register";
export default class App extends React.Component {
  render() {
    console.log("hello bros");
    return (
      <Register/>
    );
  }
}
const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: '#fff',
    alignItems: 'center',
    justifyContent: 'center',
  },
});

我不熟悉该库,但是,textComponent接受一个函数,所以你应该能够做到这一点

textComponent={
  ()=>{
  return(<TextInput 
  ....some props here 
  onFocus={()=>console.log('focused')})
  }}

相关内容

  • 没有找到相关文章