对象对象返回本机反应

时间:2020-04-29 09:13:21

标签: javascript reactjs react-native

我正在尝试从AsyncStorage获取数据,但是它返回对象对象,我如何才能正常返回数据? 如果我在console.log()中返回数据,一切正常,但是当我调用函数时,它将返回对象对象。

谢谢

const getTheme = async () => {
  try {
    const value = await AsyncStorage.getItem('theme');
    console.log(value);   // returns dark 
   return JSON.stringify(value)
  } catch (error) {
    alert(error)
  }
};

alert(getTheme()) // [object, object]

const TabNavigator = createBottomTabNavigator(
  {
    Home: {
      screen: Home,
      navigationOptions: ({ navigation }) => ({
        tabBarLabel: "Home",
        tabBarVisible: getActiveRoute(navigation.state) !== "ArticleScreen",
        tabBarIcon: ({ tintColor }) => (
          <Icon name="ios-home" color={tintColor} size={24} />
        )
      })
    },

    Courses: {
      screen: Courses,
      navigationOptions: {
        tabBarLabel: "Courses",
        tabBarIcon: ({ tintColor }) => (
          <Icon name="ios-school" color={tintColor} size={24} />
        )
      }
    }
  },
  {}
)

export default createAppContainer(TabNavigator);

class App extends React.Component {
  render() {
    return <TabNavigator />;
  }
}

1 个答案:

答案 0 :(得分:0)

尝试将其分类吗?

console.log(JSON.stringify(apiRequest1));

MDN stringify

-----根据您的新更新---- 您是否从数据中获得了黑暗(主题?)?

您可以尝试以下方法:

const getTheme = async () => {
  try {
    const value = await AsyncStorage.getItem('theme');
    console.log(value);   // returns dark (Is here a theme?)
   return value  //Just retrun the value of dark?
  } catch (error) {
    alert(error)
  }
};

alert(getTheme) //Is here have to use getTheme not getTheme()?

您可以尝试。