Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

123
Visualizações
useContext does not store my data and gives me the default one even set

i have a problem , i've set my useContext with useState values in the provider , but when i import them and try to change the value , it change locally and not globally !

Login.js

import { UserContext } from './context/UserContext';


const loginD = (setStatue , username , password, user , setUser) => {
        axios.post("http://localhost:5050/login", {
            username : username,
            password : password
        }).then(response =>{
            if(response.data.statue)
            {
                setStatue(response.data.statue);
            }
            if(response.data.message)
            {
                setUser({
                    ...user,
                    _id : response.data.message[0].id,
                    username : response.data.message[0].username,
                    password : response.data.message[0].password,
                    name : response.data.message[0].name
                })
                console.log(user);//{id : "the id", username : "username" , password : "password"}
                localStorage.setItem("jwt", "logged in");
                setStatue("Succesfully Logged in");
            }
        })
}

Home.js

const Home = () => {
    const [user , setUser] = useContext(UserContext);
    return (
        <div>
            {console.log(user)} //{id:"",username:"",password:""}
            <p>Hello {user.username}</p>
        </div>
    )
}

UserContext.js

import React , {useState ,createContext} from "react";


export const UserContext = createContext({});

export const UserProvider = (props) => {
    const [user , setUser] = useState(
        {
            _id : "",
            username : "",
            password : "",
            name : ""
        }
    );
    return(
        <UserContext.Provider value = {[user, setUser]}>
            {props.children}
        </UserContext.Provider>
    );
};

it actually gives me the default values of the useState variable can someone explain to me what actually wrong ! thanks

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

I suggest rereading the useContext documentation. While it isn't clear from your question, I have the feeling you're missing UserContext.Provider and aren't using contexts properly. You also mention you're using useState but you don't show us where.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda