我是新手,我正在尝试将一些数据作为参数发送到history.push
.
基本上,我在单击按钮时调用一个方法,在该方法内部调用一个 api。如果我得到成功响应,我会重定向到其他页面,并且我还需要传递一些数据。
下面是我的代码:
class Login extends Component {
constructor(props) {
super(props);
this.state = {
enteredName: null,
enteredPwd: null,
rescode: null,
userName: null,
formatDesc: null,
userFormat : null,
success: false,
responseJson : null,
};
}
state = {
enteredName: null,
enteredPwd: null,
rescode: null,
userName: null,
formatDesc: null,
userFormat : null,
success: false,
responseJson : null,
}
render=() =>{
return (
<Router>
<div id= "login-page">
<div className="back-image" style={{height:"100vh"}}>
<div className="container">
<form className="form-login" action="index.html">
<h2 className="form-login-heading">sign in now</h2>
<div className="login-wrap">
<label>User ID</label>
<input type="text" ref="usrname" className="form-control" placeholder="User ID" autofocus/>
<br/>
<label>Password</label>
<input type="password" ref = "password" className="form-control" placeholder="Password"/>
<br/>
<a className="btn btn-theme btn-block" onClick={this.login.bind(this)}><i className="fa fa-lock mr-10"/>SIGN IN</a>
<Dialog ref={(component) => { this.dialog = component }} />
</div>
</form>
</div>
</div>
</div>
</Router>
);
}
login = (e) => {
e.preventDefault();
fetch('some url', {
method: 'POST',
body: JSON.stringify({
"userId": this.refs.usrname.value,
"password": this.refs.password.value
})
})
.then((response) => response.json())
.then((responseJson) => {
console.log(`response: ` , responseJson)
if (responseJson.errorCode === '00') {
this.setState({ rescode: responseJson.errorCode })
this.setState({ userName: responseJson.userName })
this.setState({ formatDesc: responseJson.formatDesc });
this.setState({userFormat : responseJson.userFormat});
this.setState({success : true});
this.setState({responseJson: responseJson});
this.props.history.push(
'/Taskactive',
{
role_id : this.userFormat,
userName : this.userName
}
);
}
else {
alert("Error Logging in.." + responseJson.errorMsg);
this.refs.usrname.value = "";
this.refs.password.value = "";
}
})
.catch((error) => {
console.error(error);
});
this.refs.usrname.value = "";
this.refs.password.value = "";
}
所以到目前为止一切都很好,但是现在我需要 在下一页(即Taskactive )中读取传递的数据,即role_id和userName。那么我们如何读取这些数据呢?Taskactive.jsx