我已经在全局范围内定义了初始状态对象,如下所示。
//states related code
const [newConfigState, setNewConfig] = useState({config: {}});
//force update states
const [newForceUpdate, setNewForceUpdate] = useState({ forceUpdate:false, popupCancelable:false});
现在在 useEffect() 方法中,我调用了一些函数,它将执行一些后端 api 调用,并根据我需要设置状态对象的响应,我尝试如下。
useEffect(() => {
checkConfigStorage(setNewConfig, newConfigState, setNewForceUpdate, newForceUpdate);
}, []);
- 这是传递给函数以设置状态的正确方法吗?
现在里面的代码checkConfigStorage(setNewConfig, newConfigState, setNewForceUpdate, newForceUpdate)
如下:
export async function checkConfigStorage(setNewConfig, newConfigState, setNewForceUpdate, newForceUpdate) {
AsyncStorage.getItem(Constants.CONFIG)
.then(configstr => {
// console.log("CNG CALLED GETITEM", configstr);
//console.log("GLOBAL CONFIG", global.config);
let config;
if (configstr != null) {
config = JSON.parse(configstr);
console.log(config);
if (config !== null &&
config.data !== null &&
config.data !== undefined) {
**setNewConfig({...newConfigState, config: {config}});**
if (
global.config === undefined &&
newConfigState.data !== null
) {
global.config = newConfigState.data;
}
console.log("newConfig", newConfigState);
let userInfoObj = {};
userInfoObj.userid = userid;
userInfoObj.sessionid = sessionid;
if (newConfigState.data != null && newConfigState.data.update.version_code > AppC.current_version_code) {
//show popup
**setNewForceUpdate(...newForceUpdate, {
forceUpdate: newConfigState.data.update.force_update,
popupCancelable: newConfigState.data.update.allow_cancel
});**
}
} else {
global.config = backupConfig;
**setNewConfig(...newConfigState, {config: backupConfig});**
console.log("CONFIG stored in AsyncStorage seems NULL", config.data);
console.log("So getting it from backup and setting to state", newConfigState.data);
}
}
})
.catch(err => {
console.log("HOME 1 Error", err);
var error = {
err: err,
msg: "Error : HomeScreen: checkconfig"
};
global.config = backupConfig;
**setNewConfig({...newConfigState, {config: backupConfig}});**
console.log("Caught in the catch block, assigning backup config", newConfigState.data);
// MyEventLogger.logEventAndDesc("ERROR", error);
});
}
但问题是新值被分配给状态对象。当我尝试在控制台中打印状态对象值时,它显示如下,我在开始时初始化了哪个空对象。
newConfig {"config": {}}
您能否就如何实现这一目标提供帮助或提供一些指导?