3

I have a functional Component that has the following code.

import { isUserAuthCheck } from "../../Utils/SessionStorage"; function DefaultPage({ history }) { useEffect(() => void (isUserAuthCheck ? history.push("/home") : null)); 

and in my Utils/SessionStorage, I have the following

export const isUserAuthCheck = sessionStorage.getItem("isUserAuthenticated") === "true"; export const getIsUserAuthenticated = sessionStorage.getItem( "isUserAuthenticated" ); export const setValueInSession = (key, value) => sessionStorage.setItem(key, value); export const setIsUserAuthenticated = str => sessionStorage.setItem("isUserAuthenticated", str); 

Problem here is, isUserAuthCheck doesn't seem to update every time there's a change in sessionStorage. How can I make sure that it's getting updated every time there's a change in sessionStorage.

Any help is appreciated.

1 Answer 1

3

Because it is not a function. It doesn't fire the checking everytime you call it. You have to make it a function to run the checking AGAIN.

export const isUserAuthCheck = () => sessionStorage.getItem("isUserAuthenticated") === "true"; 

Or you can update it everytime you set value for isUserAuthenticated, but it is a const so you'd have to change it.

EDIT:

If you want it to be a getter as i mentioned:

enter image description here

Also avoid using "true" or any kind of these things. There is a reason why bool type is available

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you, that solved the issue. I believe the latter is the good approach though.
I would say the first. You don't want it to be dependent on other places as that value can be replaced by any other function which can never happen if it is a function. If you want it to be an object-oriented-like approach getter, you can declare a getter at your export default so you don't have to set the value as well everytime you change your Session Value

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.