I do have some noobie question. I need to get a value from localstorage, but I am using some specifc methods that we have in our app. I am setting my value with:
public setValue = async (key: keyof StorageContent, val: any) => {
if (!localStorage) {
console.warn("StorageManager: localStorage is not defined");
return;
}
// Let's convert `undefined` values to `null` to get the value consistent
if (val === undefined) {
val = null;
} else {
val = JSON.stringify(val);
}
return localStorage.setItem(key, val);
};
After this is done I am using
static getBackendVersion() {
return StorageManager.getValue("backendVersion");
}
getValue
public getValue = async (key: keyof StorageContent) => {
if (!localStorage) {
console.warn("StorageManager: localStorage is not defined");
return undefined;
}
const item = localStorage ? localStorage.getItem(key) : null;
if (!item || item === "null") {
return undefined;
}
try {
return JSON.parse(item);
} catch (e) {
return undefined;
}
};
In my react component where I am using state and component id mount to get this done
state: Readonly<IMainScreenState> = {
screenKey: RouteHelper.getScreenRouteKey(this.props.match),
apiVersion: "",
};
public componentDidMount() {
const backendVer = StorageHelper.getBackendVersion();
this.setState({ apiVersion: backendVer });
}
After this i want to render this
<aside className="Version">
<span>F:{AppConfig.PlatformVersion} / </span>
<span> B:{apiVersion}</span>
</aside>
But this is getting me error Objects are not valid as a React child (found: [object Promise]). If you meant to render a collection of children, use an array instead.
In conolse log i have
i know that i can get this with localStorage.getItem() but we need to use this helpers to get this done clean