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( <React.StrictMode> <App /> </React.StrictMode> );
客户端.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 });
我在App中导入客户端,看来localStorage应该在client.js运行之前设置Item。但我的 localStrorage.getItem('obj') 为 null。有谁知道为什么吗?
使用 useEffect()