[英]how can i change the status of the username in reactjs using axios?
i'm new on react and i'm building a react website, i came across a doubt on how to show the username of the person who logged in my page and i would like when the person logs in, the state change from 'hi, sign in' to 'hello x person':我是 react 新手,我正在建立一个 react 网站,我对如何显示登录我的页面的人的用户名有疑问,我想当这个人登录时,state 从“嗨”更改, 登录到 'hello x person':
My backend login: (express)我的后台登录:(快递)
app.get("/login", (req, res) => {
if (req.session.user) {
res.send({ loggedIn: true, user: req.session.user });
} else {
res.send({ loggedIn: false });
}
});
app.post("/login", (req, res) => {
const username = req.body.username;
const password = req.body.password;
db.query(
"SELECT * FROM credenziali WHERE username = ?;",
username,
(err, result) => {
if (err) {
res.send({ err: err });
}
if (result.length > 0) {
bcrypt.compare(password, result[0].password, (error, response) => {
if (response) {
req.session.user = result;
console.log(req.session.user);
res.send(result);
} else {
res.send({ message: "Wrong username/password combination!" });
}
});
} else {
res.send({ message: "User doesn't exist" });
}
}
);
});
My navbar page with axios, i tried to put the states and the call em' into navlink:我的导航栏页面带有 axios,我试图将状态和调用 em' 放入导航链接:
import React, { useState, useEffect } from 'react';
import { IconContext } from 'react-icons/lib';
import { FaBars, FaTimes } from 'react-icons/fa';
import { SignIn } from '../Buttons/Button/Button';
import Axios from 'axios';
import { Link } from 'react-router-dom';
import styled from 'styled-components'
import './Navbar.css';
import Horizzontal from './Dropdown/Dropdown';
import { useStateValue } from "../../StateProvider";
function Navbar() {
const [{ basket, user }, dispatch] = useStateValue();
const [username, setUsername] = useState("");
const [password, setPassword] = useState("");
const [loginStatus, setLoginStatus] = useState("");
const login = () => {
Axios.get("http://localhost:3001/login", {
username: username,
password: password,
}).then((response) => {
if (response.data.message) {
setLoginStatus(response.data.message);
} else {
setLoginStatus(response.data[0].username);
}
});
};
const [click, setClick] = useState(false);
const [dropdown, setDropdown] = useState(false);
const handleClick = () => setClick(!click);
const closeMobileMenu = () => setClick(false);
const onMouseEnter = () => {
if (window.innerWidth < 960) {
setDropdown(false);
} else {
setDropdown(true);
}
};
const onMouseLeave = () => {
if (window.innerWidth < 960) {
setDropdown(false);
} else {
setDropdown(false);
}
};
return (
<>
<IconContext.Provider value={{ color: '#fff' }}>
<Nav>
<NavLink to='/Login' className='navbar-logo' onClick={closeMobileMenu} onChange={login}>
{!username ? 'Hi, Sign In!' : {setLoginStatus}}
</NavLink>
<div className='menu-icon' onClick={handleClick}>
{click ? <FaTimes /> : <FaBars />}
</div>
<NavMenu>
<ul className={click ? 'nav-menu active' : 'nav-menu'}>
<li className='nav-item'>
<Link to='/' className='nav-links' onClick={closeMobileMenu}>
Home
</Link>
</li>
<li
className='nav-item'
onMouseEnter={onMouseEnter}
onMouseLeave={onMouseLeave}
>
<Link
to='/services'
className='nav-links'
onClick={closeMobileMenu}
>
Services <i className='fas fa-caret-down' />
</Link>
{dropdown && <Horizzontal />}
</li>
<li className='nav-item'>
<Link
to='/ourwb'
className='nav-links'
onClick={closeMobileMenu}
>
Our Website
</Link>
</li>
<li className='nav-item'>
<Link
to='/contact-us'
className='nav-links'
onClick={closeMobileMenu}
>
Contact Us
</Link>
</li>
<li>
<Link
to='/Login'
className='nav-links-mobile'
onClick={closeMobileMenu}
>
Sign In
</Link>
</li>
</ul>
</NavMenu>
<NavBtn>
<SignIn/>
</NavBtn>
<NavLink to='/checkout' className='cart-logo'><i class="fal fa-shopping-cart"></i></NavLink>
</Nav>
</IconContext.Provider>
</>
);
}
export default Navbar;
so after that, i want that after logging in change the state from 'hi, sign in' to 'hello x person'.所以在那之后,我希望在登录后将 state 从“嗨,登录”更改为“你好 x 人”。 I put the axios.get and then the states that i called into navlink, unfortunately it doesn't work, how could fix that?
我把 axios.get 然后我调用的状态放入 navlink,不幸的是它不起作用,如何解决? thanks!!
谢谢!!
This这个
{!username ? 'Hi, Sign In!' : {setLoginStatus}}
should be like below.应该如下所示。
setLoginStatus
is a function, you need the value to be printed setLoginStatus
是 function,需要打印的值
{!username ? 'Hi, Sign In!' : loginStatus}
and don't send user credentials in GET, use POST并且不要在 GET 中发送用户凭据,请使用 POST
Still if you are not getting check what do you have in response.data[0].username
如果你没有得到检查你在
response.data[0].username
中有什么
You are checking setting userName state to be "", but never setting it anywhere.您正在检查将 userName state 设置为“”,但从不在任何地方设置它。
setUsername
is not called anywhere. setUsername
不会在任何地方调用。 Instead of代替
{!username ? 'Hi, Sign In!' : {setLoginStatus}}
You should use你应该使用
{ !loginStatus ? 'Hi, Sign In!' : loginStatus }
// or simply
{ loginStatus || 'Hi, Sign In!' }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.