Javascript 新组件的布线和使用道具
我有一个组件,它调用一个API,如果API返回某个结果,它应该引用/welcome页面(它是welcome.js)。为此,我想通过电子邮件。是否有一个选项,我可以重定向到其他组件,同时传递道具电子邮件Javascript 新组件的布线和使用道具,javascript,reactjs,react-router,Javascript,Reactjs,React Router,我有一个组件,它调用一个API,如果API返回某个结果,它应该引用/welcome页面(它是welcome.js)。为此,我想通过电子邮件。是否有一个选项,我可以重定向到其他组件,同时传递道具电子邮件 const handleLogin = () => { axios.post('localhost:3000:/login', {"email":email, "password":password}) .then(
const handleLogin = () => {
axios.post('localhost:3000:/login', {"email":email, "password":password})
.then(res => {
console.log(res);
console.log(res.data);
console.log(res.status);
if(res.status === 200) {
setValidationWrong(true);
// Forward to another component and pass on the email
// <Link to="/welcome"> </Link>
}
})
.catch(error => {
console.log(error)
setValidationWrong(false);
})
};
consthandlelogin=()=>{
post('localhost:3000:/login',{“email”:email,“password”:password})
。然后(res=>{
控制台日志(res);
console.log(res.data);
控制台日志(res.status);
如果(资源状态===200){
SetValidationError(true);
//转发到另一个组件并传递电子邮件
//
}
})
.catch(错误=>{
console.log(错误)
SetValidationError(假);
})
};
Welcome.js
import React from "react";
import Testchat from "../components/Testchat";
const Welcome = (props) => {
return (
<div>
<h1>Welcome{props.email}</h1>
<Testchat></Testchat>
</div>
);
}
export default Welcome
从“React”导入React;
从“./components/Testchat”导入Testchat;
康斯特欢迎=(道具)=>{
返回(
欢迎{props.email}
);
}
导出默认欢迎
是的,有。使用历史API时,可以发送位置而不是字符串
e、 g。
同样的情况也适用于链接组件:
在欢迎组件中,您可以通过props.location.state.email
访问它
const location = {
pathname: '/welcome',
state: { email: foo@baz.com }
};
history.push(location);
<Link
to={{
pathname: "/welcome",
state: { email: foo@baz.com }
}}
/>