未捕获错误:对象作为 React 子对象无效(发现:[object Promise])
未捕获错误:对象作为 React 子对象无效(发现:[object Promise])。
import React, { useState,useEffect } from 'react';
import './App.css';
async function Login() {
const [username,setUsername] = useState("")
const [password,setPassword] = useState("")
async function handleSubmit({event) {
event.preventDefault
fetch("http://localhost:3001/api/login",{
method:"post",
headers:{
"Content-Type" : "application/json"
},
body:JSON.stringify({username:username,password:password})
})
const data = await response.json()
console.log(data.username)
}
return (
<div className="App">
<form onSubmit={handleSubmit}>
<input placeholder="username" value={username} onChange={ e => setUsername(e.target.value)} />
<input placeholder="password " value={password} onChange={ e => setPassword(e.target.value)}/>
<button type='submit' >Submit</button>
</form>
</div>
);
}
export default Login;
Uncaught Error: Objects are not valid as a React child (found: [object Promise]).
import React, { useState,useEffect } from 'react';
import './App.css';
async function Login() {
const [username,setUsername] = useState("")
const [password,setPassword] = useState("")
async function handleSubmit({event) {
event.preventDefault
fetch("http://localhost:3001/api/login",{
method:"post",
headers:{
"Content-Type" : "application/json"
},
body:JSON.stringify({username:username,password:password})
})
const data = await response.json()
console.log(data.username)
}
return (
<div className="App">
<form onSubmit={handleSubmit}>
<input placeholder="username" value={username} onChange={ e => setUsername(e.target.value)} />
<input placeholder="password " value={password} onChange={ e => setPassword(e.target.value)}/>
<button type='submit' >Submit</button>
</form>
</div>
);
}
export default Login;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你忘记在调用方法之前放置等待
you forgot put await before calling method