I have a component provided by an external library which I need to provide an access token to. This access token is saved in localStorage at this point and I would like to read it and pass it to the component. This is my attempt using useEffect but this fails with the error {code: "initialization-error", error: "Access token required"}
const test: NextPage = () => {
const router = useRouter()
let accessToken = ''
useEffect(() => {
const at = localStorage.getItem('accessToken')
console.log(at) // prints out the correct access token
if (at !== null) {
accessToken = at
}
}, [])
return (
<WebSdk
accessToken={accessToken}
expirationHandler={() => Promise.resolve(accessToken)}
onError={(error) => {
console.log(error)
}}
/>
)
}
export default test
I'm guessing the useEffect hook runs after the component renderes - how can I read from local storage before or as the component renders?