Fair warning, I am newbie REACT developer. I am coming from .NET world. I have taken week long training courses on Pluralsight and built some simple demo apps. I am posting this question after several days of google searches and reading numerous articles. I dont have any other REACT devs on our team, so I am kind of stuck and hoping the community here can help me out?
Currently at work I am building a REACT web app that has several pieces of functionality. The app is a web app to demo OAUTH flows with Azure B2C. I have multiple buttons on my app that demonstrate implicit flow, auth flow, refresh token etc. When each of these buttons are clicked the user is redirected to the OAUTH endpoints for login etc. Thus the user is redirected away from my REACT app. In order to store some of the choices the user made on the form and to hold onto the OAUTH token and other values, I am storing values in REACT state.
this.state = {
fieldName1: localStorage.getItem("FieldName1"),
fieldName2: localStorage.getItem("FieldName2"),
fieldName3: localStorage.getItem("FieldName3"),
fieldName4: localStorage.getItem("FieldName4"),
fieldName5: localStorage.getItem("FieldName5"),
};
As you can imagine once the redirection happens away from my REACT app the state values are lost. So I looked into using cookies. I then came across some articles on using local storage. That is currently what I have in my code. My issue comes in when the user is redirected back to my app I am unable to reload values from state. When I run the app in Chrome and I look at the dev tools panel and look at my local storage, all five of my values are present. However, all five of my state values are NOT getting reloaded, from local storage. In my app I end up calling "setState" multiple times, as the user clicks different buttons and different "events" take place. I read this thread and then consolidated all of my setState calls into one function.
updateStateValuesAndLocalStorage(name, value) {
this.setState(
{
name: value
},
updatedState => {
console.log("updatedState: ", updatedState);
}
);
localStorage.setItem(name, value);
}
I also read this thread so I know that the asynchronous thing is probably causing my issues. But being a newbie I am struggling to solve this. Any help or guidance here would be much appreciated!