简体   繁体   English

如何使用 React Native 和 AWS 确保每次都导入我的个人资料信息?

[英]How do I make sure my Profile information is imported every time, using React Native and AWS?

I have a RN/Expo app.我有一个 RN/Expo 应用程序。 The user can sign in and push the drawer (hamburger) button, and see their profile information.用户可以登录并按下抽屉(汉堡包)按钮,并查看他们的个人资料信息。 This info is pulled from an AWS server, but it is really hit or miss as to whether or not the information renders/is displayed.此信息是从 AWS 服务器中提取的,但对于信息是否呈现/显示而言,它确实受到影响。 I would like it to display all the time, but that never seems to be a guarantee.我希望它一直显示,但这似乎永远不能保证。

Below is my code:下面是我的代码:

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

}
        )
       },
      [],
    )

... //Inside the return statement on the drawer, where it should be rendered on opening ... //在抽屉上的返回语句中,它应该在打开时呈现

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

EDIT编辑

    //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)
    }

Edit for Photos:编辑照片:

    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>

您可以使用then解析返回的承诺并调用内部的getFirstName方法。

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

I'm wondering if your function is getting called multiple times and possibly overwriting the previous value obtained by your function.我想知道您的函数是否被多次调用并可能覆盖您的函数获得的先前值。 Even if this isn't the case, it's good practice to only bind these server obtained information if real_response is not undefined.即使情况并非如此,如果real_response未定义,最好只绑定这些服务器获得的信息。 You can also remove your unused dependency array.您还可以删除未使用的依赖项数组。

export function DrawerContent(props) {
  const [firstName, getFirstName] = useState("");

  useEffect(() => {
    async () => {
      const displayName = async () => {
        try {
          const real_response = await client_instance.get_user_info(); //server function that calls user's info from the AWS server
          if (real_response) {
            getFirstName("");
          } else {
            console.log("Could not fetch info from server.");
          }
        } catch (err) {
          console.log(`Error occurred fetching from server: ${err.message}`);
        }
      };
      displayName();
    };
  });
}

While some of these answers helped, the thing that truly fixed this issue was caching the values after the user initially signs in and storing them for future use.虽然其中一些答案有所帮助,但真正解决此问题的是在用户最初登录后缓存值并存储它们以备将来使用。 Additionally, in case the initial response returns null, the server will keep trying get the data, and won't cache until data has been received.此外,如果初始响应返回 null,服务器将继续尝试获取数据,并且在接收到数据之前不会缓存。

Here is an example:下面是一个例子:

User signs up => User signs in => Profile info populated => Data Cached用户注册 => 用户登录 => 配置文件信息填充 => 数据缓存

User signs up => User signs in => Profile info doesn't populate => Server checks again until data retrieved => Data cached用户注册 => 用户登录 => 个人资料信息不会填充 => 服务器再次检查,直到检索到数据 => 数据缓存

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

相关问题 如何确保使用React的第一个渲染具有localStorage的值? - How do I make sure my first render with React has a value from localStorage? 如何在不使用JSX的情况下在React Native中创建组件? - How do I make components in React Native without using JSX? 如何确保页面已加载且信息已更改,以便我的爬虫不会爬取相同的数据? - How do I make sure the page is loaded and the information has changed so my crawler wouldn't crawl the same data? 如何确保我的函数对我的变量有效? - How do I make sure my function works for my variable? 确保我的.JS文件每次都加载到其他人之前 - Make sure my .JS file loads every time before others 如何确保我的Ajax调用始终成功? - How do I make sure my ajax calls are always successful? 一旦用户使用 React 和 Firebase 登录(从“登录”到“个人资料”),我如何更新我的导航栏? - How do I update my NavBar once a user is logged-in (from 'log in' to 'profile') using React & Firebase? 使用 React,如何使变量的值动态化? - Using React, how do I make the value of my variable dynamic? 每次调用函数(JavaScript)时,如何检查if / else语句? - How do I make the if/else statement get checked every time my function is called (JavaScript)? 如何使用JavaScript + jQuery确保URL是图像? - How do I make sure a URL is an image using JavaScript + jQuery?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM