简体   繁体   English

没有从函数[React Native]获得任何返回值

[英]Not getting any return value from function [React Native]

I have this menu where I filter the button when user is logged and I'm accessing a function from a separate file to check out if user is logged or not. 我有这个菜单,我在用户登录时过滤按钮,我从一个单独的文件访问一个功能,检查用户是否被记录。

From menu 从菜单

import { IsLogged } from '../GlobalFunctions';

const SubDrawer1 = ({ data, onChange }) => (
    <View>
    {
        IsLogged() ? (
            <View>
                <TouchableOpacity onPress={()=>{onChange('LogoutScreen')}}>
                    <Text>Logout</Text>
                </TouchableOpacity>
            </View>
        ) : (<View />)
    }     
    </View>
);
export default SubDrawer1;

From GlobalFunctions 来自GlobalFunctions

export function IsLogged(){
    AsyncStorage.getItem('userId').then((userId) => {
        return userId ? true : false
    })
}

From menu I call out IsLogged() function but the value is undefined when I console.log it. 菜单中我调用了IsLogged()函数,但是当我调试它时,该值是undefined It supposed to return either true or false . 它应该返回truefalse

because AsyncStorage will take some time, and before that function completes its execution. 因为AsyncStorage需要一些时间,并且在该功能完成执行之前。 (Because everything in JS is asynchronous) (因为JS中的所有内容都是异步的)

So just make it wait until it finishes getting an item, 所以只要让它等到它完成一个项目,

export function async IsLogged(){
    let userId = await AsyncStorage.getItem('userId');
    return userId ? true : false
}

You are trying to call a async function, so you need to add some extra code. 您正在尝试调用异步函数,因此您需要添加一些额外的代码。 Try the following. 请尝试以下方法。

SubDrawer1 SubDrawer1

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

import React, { Component } from "react";
import { IsLogged } from "../GlobalFunctions";

const SubDrawer1 = async ({ data, onChange }) => {
  const status = await IsLogged();
  if (status === true) {
    return (
      <View>
        <View>
          <TouchableOpacity
            onPress={() => {
              onChange("LogoutScreen");
            }}
          >
            <Text>Logout</Text>
          </TouchableOpacity>
        </View>
      </View>
    );
  } else {
    return (
      <View>
        <View />
      </View>
    );
  }
};

export default SubDrawer1;

'IsLogged()' Global function 'IsLogged()'全局函数

export function async IsLogged(){
    let userId = await AsyncStorage.getItem('userId');
    return userId ? true : false
}

'SubDrawer1' using class 'SubDrawer1'使用类

...

import SubDrawer1 from "./SubDrawer1"; // import your 'SubDrawer1' component

var subDrawer1Component; // declare a variable for your rendering component

//Your main class
export default class ClassName extends Component<Props> {

  componentDidMount() {
    this.getComponentInfo(); //Call this method to get function
  }

  getComponentInfo = async () => {
    subDrawer1Component = await SubDrawer1();// Will take the component
    this.setState({ isDataGet: true });
  };

  render() {
    return (
        ...

        {subDrawer1Component}

        ...
    );
  }

}

...

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

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