index.js:
import React from 'react';
import ReactDOM from 'react-dom/client';
import './index.css';
import App from './App';
import reportWebVitals from './reportWebVitals';
const root = ReactDOM.createRoot(document.getElementById('root'));
const obj = { a: "xxx", b: "yyy" }
localStorage.setItem('obj', JSON.stringify(obj));
root.render(
);
Client.js:
let settings = JSON.parse(localStorage.getItem('obj'));
const oktaAuth = new OktaAuth({
issuer: settings['a'],
clientId: settings['b'],
redirectUri: window.location.origin + '/login/callback',
logoutUrl: window.location.origin + '/login',
// pkce: false
});
I imported the client in App, it seems that localStorage should set Item before client.js is run. But my localStrorage.getItem('obj') is null. Does anyone know why?
Your Answer
1 answers
Use useEffect()
import React, {useEffect} from 'react';
const App = () => {
useEffect(() => {
const obj = { a: "xxx", b: "yyy" }
localStorage.setItem('obj', JSON.stringify(obj));
},[])
return (
)
}
Hot tools Tags
Hot Questions
jquery IE Fadein and Fadeout Opacity
2025-12-29 22:43:11
How to access the request body when POSTing using Node.js and Express?
2025-12-29 22:22:13
What are the differences between Deferred, Promise and Future in JavaScript?
2025-12-29 22:01:40
Sending "User-agent" using Requests library in Python
2025-12-29 21:43:37
How do MySQL indexes work?
2025-12-29 21:22:13
Popular tool
vc9-vc14 (32+64 bit) runtime library collection (link below)
Download the collection of runtime libraries required for phpStudy installation
VC9 32-bit
VC9 32-bit phpstudy integrated installation environment runtime library
PHP programmer toolbox full version
Programmer Toolbox v1.0 PHP Integrated Environment
VC11 32-bit
VC11 32-bit phpstudy integrated installation environment runtime library
SublimeText3 Chinese version
Chinese version, very easy to use
Hot Topics
Douyin level price list 1-75
20416
7
20416
7
wifi shows no ip assigned
13573
4
13573
4






