cari

Rumah  >  Soal Jawab  >  teks badan

useState dalam ReactJS tidak berfungsi semasa mengemas kini objek

Saya mempunyai penyedia konteks pengesahan berikut. Selepas pengguna berjaya mengesahkan, saya menetapkan beberapa nilai token

import axios from "axios";
import { createContext, useContext, useState } from "react";
import Constants from "../common/Constants";
import Payload from "../classes/Payload";

const AuthenticationContext = createContext({
    loggedUserToken: {},
    session: {}
});

export const useAuthenticationStatus = () => useContext(AuthenticationContext);

const AuthenticationContextProvider = ({children}) => {

    const [loggedUserToken, setLoggedUserToken]     = useState({});
    const [session, setSession]                     = useState({});


    const authenticateUser = async (loginEndpoint, email, userPassword) => {         

        let result                                  = {};
        let statusCode                              = -1;
        let statusText                              = '';
        let message                                 = '';
        let session                                 = '';
        let postData                                = {username: email, password: userPassword};

        await axios.post(loginEndpoint, postData, {headers: headerData})
                .then( data => {

                    statusCode                      = data.data.code;
                    statusText                      = data.data.statusText;
                    session                         = data.data.session;
                    message                         = data.data.message;

                    if (statusCode === 200) {                

                        let challengeName           = message.challengeName;
                        let payloadData             = '';
                        
                        if (session !== null && message.challenge !== null) {

                            let userEmail           = message.userEmail;
                            setSession({'sD': session, 'sE': userEmail });
                            
                            if (challengeName === Constants.COGNITO_CHALLENGE_NEW_PASSWORD_REQUIRED ) {
                                statusText          = Constants.AUTHENTICATION_SET_NEW_PASSWSORD;
                                payloadData         = challengeName;
                            }

                            result = { 's': true, 'sT': statusText, 'p': payloadData};

                        } else {
                            statusText              = Constants.AUTHENTICATION_LOGIN_SUCCESSFUL;
                            payloadData             = message;

                            let userpayloadData      = new Payload(payloadData);

                            //set use state for user token - ERROR IN HERE
                            //loggedUserToken IS NOT SET HERE
                            setLoggedUserToken(loggedUserToken => userpayloadData);

                            result = { 's': true, 'sT': statusText, 'p': payloadData};

                        }                        

                    }

                }).catch( error => {
                    
                    result = { 's': false, 'sT': Constants.COMMON_ERROR_MESSAGE, 'p': ''};

                });

        return result;  
    }

    

    return (
        <AuthenticationContext.Provider
            value={{loggedUserToken, authenticateUser, session, setPermanentPassword}}>
            {children}
        </AuthenticationContext.Provider>
    )
};

export default AuthenticationContextProvider;

Saya menggunakan nilai "logUserToken" dalam paparan rumah untuk menyemak sama ada ia tersedia.

import { useEffect } from "react";
import { useAuthenticationStatus } from "../../services/AuthenticateContextProvider";

function HomeView() {

    const {loggedUserToken} = useAuthenticationStatus();
    
    //empty object output
    console.log(loggedUserToken);

    useEffect( () => {
        //empty object output
        console.log(loggedUserToken);
    })

    return (

        
        <div className="col-12">
            
            <div className="row mt-2">
                <div className="col-12">

                </div>
            </div>

            <div className="row mt-5 incident-list-map">

                <div className="col-md-4">

                    <h2>List</h2>
                    
                    <div></div>

                </div>

                <div className="col-md-8">
                    <div id="map" className="map">
                        
                    </div>

                </div>

            </div>

        </div>

    );
}

export default HomeView;

"loggedUserToken" sentiasa kosong walaupun anda menetapkannya dalam pembekal pengesahan (walaupun anda mencetaknya ke konsol sejurus selepas "setLoggedUserToken" pada pembekal pengesahan). "setSession" ditetapkan dengan betul, nilai dibaca dalam paparan lain.

Saya tidak pasti apa yang salah di sini

Kemas kini: Inilah cara saya menggunakan pembekal pengesahan. "Outlet" menggantikan pandangan yang berbeza dalam MainLayout.

import { Outlet } from "react-router-dom";
import MainHeader from "../common/MainHeader";
import MainFooter from "../common/MainFooter";
import AuthenticationContextProvider from "../../services/AuthenticateContextProvider";

function MainLayout() {

    return(

        <div className="main-layout">
            <AuthenticationContextProvider>
                <header>
                    <nav className="navbar navbar-expand-md fixed-top header-area">
                        <MainHeader></MainHeader>
                    </nav>
                </header>
                
                <main className="flex-shrink-0 main-area">
                    <div className="container-fluid">
                        <div className="main-content">
                            
                                <Outlet />
                            
                        </div>
                        
                    </div>
                </main>

                <footer className="footer mt-auto py-3 footer-content">
                    <div className="container-fluid">
                        <MainFooter></MainFooter>
                    </div>
                </footer>
            </AuthenticationContextProvider>
        </div>

    );

}

export default MainLayout;

P粉986028039P粉986028039419 hari yang lalu649

membalas semua(1)saya akan balas

  • P粉283559033

    P粉2835590332023-09-22 11:31:13

    Saya dah jumpa penyelesaiannya. Ralat di atas disebabkan oleh susun atur yang saya gunakan. Saya menggunakan satu reka letak dalam paparan pengesahan (seperti log masuk, pendaftaran, dll.) dan susun atur lain dalam paparan lain (rumah, hubungi kami, dsb.).

    Saya telah merangkumnya secara individu ke dalam penyedia konteks pengesahan. Seperti yang ditunjukkan di bawah:

    import { Outlet } from "react-router-dom";
    import MainHeader from "../common/MainHeader";
    import MainFooter from "../common/MainFooter";
    import AuthenticationContextProvider from "../../services/AuthenticateContextProvider";
    
    function MainLayout() {
    
        return(
    
            <div className="main-layout">
                <AuthenticationContextProvider>
                    <header>
                        <nav className="navbar navbar-expand-md fixed-top header-area">
                            <MainHeader></MainHeader>
                        </nav>
                    </header>
                    
                    <main className="flex-shrink-0 main-area">
                        <div className="container-fluid">
                            <div className="main-content">
                                
                                    <Outlet />
                                
                            </div>
                            
                        </div>
                    </main>
    
                    <footer className="footer mt-auto py-3 footer-content">
                        <div className="container-fluid">
                            <MainFooter></MainFooter>
                        </div>
                    </footer>
                </AuthenticationContextProvider>
            </div>
    
        );
    
    }
    
    export default MainLayout;

    import { Outlet } from "react-router-dom";
    import MainHeader from "../common/MainHeader";
    import MainFooter from "../common/MainFooter";
    import AuthenticationContextProvider from "../../services/AuthenticateContextProvider";
    
    function MainLayout() {
    
    
        return(
    
            <div className="main-layout">
                
                    <header>
                        <nav className="navbar navbar-expand-md fixed-top header-area">
                            <MainHeader></MainHeader>
                        </nav>
                    </header>
                    
                    <main className="flex-shrink-0 main-area">
                        <div className="container-fluid">
                            <div className="main-content">
                                
                                    <Outlet />
                                
                            </div>
                            
                        </div>
                    </main>
    
                    <footer className="footer mt-auto py-3 footer-content">
                        <div className="container-fluid">
                            <MainFooter></MainFooter>
                        </div>
                    </footer>
    
            </div>
    
        );
    
    }
    
    export default MainLayout;

    Tetapi apabila saya mengalihkan penyedia konteks pengesahan ke dalam App.js, ia berjaya. Daripada apa yang saya faham, saya fikir apa yang berlaku ialah penyedia konteks pengesahan ditetapkan semula apabila ia bergerak dari susun atur pengesahan ke susun atur utama (dalam susun atur utama ia adalah penyedia konteks yang berasingan). Tetapi memandangkan saya mengalihkan penyedia konteks ke peringkat atas (App.js), ia kini biasa merentas semua reka letak.

    import 'bootstrap/dist/css/bootstrap.css';
    import 'bootstrap/dist/js/bootstrap.js';
    import './App.css';
    import { Route, Routes } from 'react-router-dom';
    import AuthenticationLayout from './components/layouts/AuthenticationLayout';
    import LoginView from './components/authentication/LoginView';
    import ForgotPasswordView from './components/authentication/ForgotPasswordView';
    import MainLayout from './components/layouts/MainLayout';
    import HomeView from './components/main/HomeView';
    import ConfirmEmailView from './components/authentication/ConfirmEmailView';
    import SetNewPassword from './components/authentication/SetNewPassword';
    import AuthenticationContextProvider from './services/AuthenticateContextProvider';
    
    function App() {
    
      return (
    
        <>
          <AuthenticationContextProvider>
            <Routes>
              <Route element={ <AuthenticationLayout /> }>
                <Route path='/' element={ <LoginView /> }></Route>
                <Route path='/login' element={ <LoginView /> }></Route>
                <Route path='/forgotpassword' element={ <ForgotPasswordView />}></Route>
                <Route path='/confirmemail'element={ <ConfirmEmailView /> }></Route>
                <Route path='/setnewpassword' element={ <SetNewPassword /> }></Route>
              </Route>
    
              <Route element={ <MainLayout /> }>
                <Route path='/home' element={ <HomeView /> }></Route>
              </Route>
    
            </Routes>
          </AuthenticationContextProvider>
        </>
    
      );
    
    }
    
    export default App;

    balas
    0
  • Batalbalas