I am trying to display user login info onto a React Material UI Typography label that is nested into an App bar(Header.js) by using data from another .js file(Login.js).
Here is the relevant code from the Header.js file:
<Typography color='textSecondary' className={classes.userText}>{}</Typography> // The label for the user info
and here is the data to be fetched from the Login.js file:
const [formData, updateFormData] = useState(initialFormData);
const handleChange = (e) => {
updateFormData({
...formData,
[e.target.name]: e.target.value.trim(),
});
};
const [error, setError] = useState();
const handleSubmit = (e) => {
e.preventDefault();
console.log(formData);
axiosInstance
.post(`token/`, {
email: formData.email, //the data I want to be displayed on the App Bar
password: formData.password,
})
.then((res) => {
localStorage.setItem('access_token', res.data.access);
localStorage.setItem('refresh_token', res.data.refresh);
axiosInstance.defaults.headers['Authorization'] =
'JWT ' + localStorage.getItem('access_token');
history.push('/');
//console.log(res);
//console.log(res.data);
}, reason =>{
console.error(reason);
setError("Invalid login details!")
alert("Login Failed!\nIncorrect login details!");
});
};
I am expecting to see the user email and display it in the Typography label...
import React, { createContext } from "react"; export const LoginContext = React.createContext();and I am usinguseContextto read whatever values stored increateContextabove but I don't know how to pass in values into thisReact.createContext();part so that the other file will be able to use it...