简体   繁体   English

每当该布局调用时,我如何在 class 组件中自动调用 function

[英]how Do I call function automatically in class component whenever the that layout called

I want to call displaydata function automatically whenever the other layout call this one because I want to use setUID in Api to display data in the same layout.我想在其他布局调用此布局时自动调用 displaydata function 因为我想在 Api 中使用 setUID 以在相同布局中显示数据。 I already saved the data through other layout by using asyncStorage and test it by using alert, below is the proof.我已经使用 asyncStorage 通过其他布局保存了数据,并使用 alert 对其进行了测试,下面是证明。

Image of layout that shows alert显示警报的布局图像

Here is the code:这是代码:

import React, { Component, useEffect } from 'react';

import { View,Text,Dimensions,TouchableOpacity } from 'react-native';

import axios from 'axios';

import Card from './Card';

import CardSection from './CardSection';

import AsyncStorage from '@react-native-community/async-storage';


// Create a component

class ProfileActivity extends Component {

    constructor() {
      super();
      this.state = {
        profile: [],
        setUID:'',
          };
      
    }

    displayData = async ()=>{  
        try{  
            const user = await AsyncStorage.getItem('responseJson');  
            const parsed = JSON.parse(user); 
            this.setState({setUID:parsed.UID});
            console.log(this.state.setUID);
            console.log(this.state.profile);
        }  
        catch(error){  
          alert(error)  
        }  
    }  


    UNSAFE_componentWillMount()
    {
      axios.get('https://abc/UserInfo.php?UID='+this.state.setUID)
      .then(response => this.setState({profile: response.data}) );
    }

   
      
    render() {
        
        console.log(this.state.UserID);
        return (
           
            <View style={styles.container}>
                <Card >
                <CardSection >
                    {/* <View style={styles.ViewStyle}>
                        <Text style={styles.TextStyle}>UID     {this.state.profile.UID}</Text>
                    </View>
                    <View style={styles.ViewStyle}>
                        <Text style={styles.TextStyle}>Name    {this.state.profile.CFName} {this.state.profile.CLName}</Text>
                    </View>
                    <View style={styles.ViewStyle}>
                        <Text style={styles.TextStyle}>Company {this.state.profile.CCompany}</Text>
                    </View>
                    <View style={styles.ViewStyle}>
                        <Text style={styles.TextStyle}>Phone # {this.state.profile.CTelHome}</Text>
                    </View> */}
                    <View style={styles.ViewStyle}>
                        <Text style={styles.TextStyle}>UID</Text>
                        <Text style={styles.TextStyle}>Name</Text>
                        <Text style={styles.TextStyle}>Company</Text>
                        <Text style={styles.TextStyle}>Phone #</Text>
                        <Text style={styles.TextStyle}>setUID</Text>
                    </View>
                    <View style={styles.ViewStyle}>
                        <Text style={styles.TextStyle}>{this.state.profile.UID}</Text>
                        <Text style={styles.TextStyle}>{this.state.profile.CFName} {this.state.profile.CLName}</Text>
                        <Text style={styles.TextStyle}>{this.state.profile.CCompany}</Text>
                        <Text style={styles.TextStyle}>{this.state.profile.CTelHome}</Text>
                        <Text style={styles.TextStyle}>{this.state.setUID}</Text>
                    </View>
                    <TouchableOpacity onPress ={this.displayData}>  
                        <Text>sdsd</Text> 
                    </TouchableOpacity>
                </CardSection>
            </Card> 
            </View>
        );
    }
}

export default ProfileActivity;

const {height} = Dimensions.get("screen");
const height_logo = height * 0.30;

const styles = {
    container: {
        backgroundColor: '#fff'
    },
    ViewStyle: {
        paddingTop:50,
        
    },
    TextStyle: {
        justifyContent:'flex-start',
        // alignSelf: 'center',
        color: '#000',
        fontWeight: 'bold',
        fontSize: 20,
        padding: 5, 
        maxWidth: height_logo,        

    }
}

You can use componentWillMount() function provided by React-native.您可以使用 React-native 提供的componentWillMount() function。 This function is triggered before render() function.这个 function 在render() function 之前被触发。 So, if you perform all the actions of dispalyData function inside componentWillMount() , it'll be performed everytime you call this component before render() method.因此,如果您在componentWillMount()中执行dispalyData function 的所有操作,则每次在render()方法之前调用此组件时都会执行此操作。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM