首页 > 解决方案 > 如何使用 React Native 和 AWS 确保每次都导入我的个人资料信息?

问题描述

我有一个 RN/Expo 应用程序。用户可以登录并按下抽屉(汉堡)按钮,并查看他们的个人资料信息。此信息是从 AWS 服务器中提取的,但对于信息是否呈现/显示,它确实是命中注定的。我希望它一直显示,但这似乎从来都不是保证。

下面是我的代码:

export function DrawerContent (props){

const [ firstName, getFirstName ] = useState('')

useEffect(() =>{
    (async()=>{
    
        const displayName = async () =>{
            const real_response = await client_instance.get_user_info() //server function that calls user's info from the AWS server
                getFirstName(
                    <Text style = {styles.title}> 
                    {real_response.first_name} {real_response.last_name}
                    </Text>
                ) 
            }
displayName()

}
        )
       },
      [],
    )

... //在抽屉的return语句中,它应该在打开时呈现

<View style = {{marginLeft: width*0.021, flexDirection: 'column'}}>
    <Title style = {styles.title}>{firstName}</Title>   
</View>

编辑

    //This is the final form 
const displayName = async () =>{
        const real_response = client_instance.get_user_info().then(response=>{
            
            getFirstName(
                <Text style = {styles.title}> 
                {response.first_name} {response.last_name}
                </Text>
            )
        }
        )
            console.log('response is here', real_response)
    }

编辑照片:

    const [ imageData, getImageData ] = useState(null)
        
        const displayPhoto = async () => {          
        const real_response = client_instance.download_profile_photo().then(response=>{

getImageData(
        response.raw_data
    )  
    console.log('photo is here=>',response )            
      }         
     )     
    }
        
        displayPhoto()
        
    <View>
        {imageData && <Avatar.Image source={{uri:`data:image/jpg;base64,${imageData}`}}/>}
    </View>

标签: javascriptamazon-web-servicesreact-nativereact-hooksexpo

解决方案


您可以使用then解析返回的承诺,并在其中调用getFirstName方法。

 client_instance.get_user_info().then(response => { // call getFirstName here });

推荐阅读