我正在构建一个本机应用程序,我需要为每种用户类型更改主题颜色。我从服务器提取此颜色代码并保持异步存储。

我尝试添加func来更改原色变量,但不等待就将其传递给我的代码给我保证没有价值

import { AsyncStorage } from 'react-native';

let primaryColor = "orange"

const func = async () => {
    let some;
    let thing = await AsyncStorage.getItem("profile")
    thing = JSON.parse(thing)
    console.warn("thing", thing.background)
    if (thing.background != undefined) {
        some = thing.background
    } else if (thing == undefined) {
        some = "red"
    }
    return some
}

primaryColor = func()


原版的:

let primaryColor = "orange";

let color = {
    primaryColor: primaryColor,
    darkPrimaryColor: "#C31C0D",
    lightPrimaryColor: "#FF8A65",
    accentColor: "#4A90A4"

export let BaseColor = {
    ...color,
    ... {
        textPrimaryColor: "#212121",
        textSecondaryColor: "#E0E0E1",
        grayColor: "#9B9B9B",
        darkBlueColor: "#24253D",
        dividerColor: "#BDBDBD",
        whiteColor: "#FFFFFF",
        fieldColor: "#F5F5F5",
        yellowColor: "#FDC60A",
        navyBlue: "#3C5A99"
    }
};

最佳答案

您的函数是异步的。通过异步操作获取值。



async componentDidMount(){

  primaryColor = await func()
}

07-24 19:07