我正在通过从app.js中的api加载的json更新我的应用程序布局(由reloadLayoutsConfig函数保存),当我想访问应用程序内的数据时一切正常,但是如何在我的layoutRedux文件中加载布局?
export const reloadLayoutsConfig = async (newLayoutsConfig) => {
try {
await AsyncStorage.setItem("@LayoutsConfig", JSON.stringify(newLayoutsConfig));
} catch (error) {
console.log("error save user data", error);
}
};
export const getLayoutsConfig = async () => {
try {
const LayoutsConfig = await AsyncStorage.getItem("@LayoutsConfig");
return JSON.parse(LayoutsConfig);
} catch (error) {
console.log(error);
}
};
这两个函数就像charm一样工作,只要我需要一个值,我就这样做:
getLayoutsConfig().then((LayoutsConfig) => {
this.setState({ LayoutsConfig : LayoutsConfig });
});
这是我的layoutRedux:
import { getLayoutsConfig} from '@config'
const types = {
// LOTS OF CODE
}
const initialState = {
layout: [],
isFetching: false,
}
var layouts = [];
// OLD TYPE FROM STORED JSON FILE IN CODE
var layouts = [...LayoutsConfig];
// NEW WAY FROM LOADED JSON FILE FROM API
getLayoutsConfig().then((LayoutsConfig) => {
layouts = LayoutsConfig;
});
initialState.layout = layouts;
export const actions = {
// LOTS OF CODE
}
export const reducer = (state = initialState, action) => {
// LOTS OF CODE
}
我已经提到了我用相同代码访问json文件的新旧方式,供您检查。在旧版本中,我曾经在代码中包含layout.json文件,当我需要访问文件时,我就这样使用它:
import { LayoutsConfig } from '@config'
var layouts = [...LayoutsConfig];
在配置中:
export const LayoutsConfig = AppConfig.LayoutsConfig;
但是现在我这样称呼它:
var layouts = [];
getLayoutsConfig().then((LayoutsConfig) => {
layouts = LayoutsConfig;
});
我收到类似未加载布局的错误。我该怎么办以及如何调用我的函数?