I am developing an app using javaScriptServices, with the react+redux template. I am consuming an API endpoint that returns an image as base64, this image happens to be a user avatar image. Code for cosuming endpoint:
axios.post(API_URL+"api/sample/endpoint").then(function(response){
saveAvatarLocalStorage(response.data.result);
}).catch(...)
function for saving to local storage:
function saveAvatarLocalStorage(data:any){
localStorage.setItem('avatar',data);
}
When I get the image I save it to local storage, and then i consume it from different parts of my code, and avoid API calls.
Code for getting the base64 image from localstorage:
componentDidMount(){
....
var imgB64 = userService.loadUserAvatar();
this.setState({imgAvatar: imgB64});
}
and this is how i display the image:
<img src={this.state.imgAvatar}/>
So far, so (apparently) good. The big problem I am facing is that an image sizing 1 to 2 mb can be a really huge string to save in local storage, and since localstorage calls are asynchronous, some times my app loads all the data from the api, but the local storage image is not yet ready. Can some one help me on this?