我已经使用-create-react-app安装了反应。我正在使用onSubmit()
函数提交表单。从LoginRegister.js
组件提交表单,下面是我的LoginRegister.js文件代码 -根据API反应显示消息js
import React from 'react';
import DefaultLayout from '../Layout/DefaultLayout';
import PropTypes from 'prop-types';
import { connect } from 'react-redux';
import { userSignupRequest } from '../actions/signupActions';
class LoginRegister extends React.Component {
constructor(props) {
super(props);
this.state = {first_name: '',last_name: '',email:'',password:'',c_password:'', phone_no:'',user_role:2, errmsg:''};
//this.state = {about: ''};
this.handleInputChange = this.handleInputChange.bind(this);
this.handleSubmit = this.handleSubmit.bind(this);
}
handleInputChange(event) {
this.setState({ [event.target.name]: event.target.value});
}
handleSubmit(event) {
event.preventDefault();
this.props.userSignupRequest(this.state);
}
render(){
return (
<DefaultLayout>
<section id="page-title">
<div className="container clearfix">
<h1>My Account</h1>
<ol className="breadcrumb">
<li><a href="/">Home</a></li>
<li><a href="/">Pages</a></li>
<li className="active">Login</li>
</ol>
</div>
</section>
<section id="content">
<div className="content-wrap">
<div className="container clearfix">
<div className="col_two_third col_last nobottommargin">
<h3>Dont have an Account? Register Now.</h3>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Unde, vel odio non dicta provident sint ex autem mollitia dolorem illum repellat ipsum aliquid illo similique sapiente fugiat minus ratione.</p>
<p id="msg">{this.state.errmsg}</p>
<form id="register-form" name="register-form" className="nobottommargin" method="post" onSubmit={this.handleSubmit}>
<div className="col_half">
<label htmlFor="register-form-name">First Name:</label>
<input type="text" id="register-form-firstname" name="first_name" value={this.state.first_name} className="form-control" onChange={this.handleInputChange} />
</div>
<div className="col_half col_last">
<label htmlFor="register-form-email">Last name:</label>
<input type="text" id="register-form-lastname" name="last_name" value={this.state.last_name} className="form-control" onChange={this.handleInputChange} required={true} />
</div>
<div className="clear"></div>
<div className="col_half">
<label htmlFor="register-form-email">Email Address:</label>
<input type="text" id="register-form-email" name="email" value={this.state.email} className="form-control" onChange={this.handleInputChange} required={true} />
</div>
<div className="col_half col_last">
<label htmlFor="register-form-repassword">Phone no.:</label>
<input type="text" id="register-form-phone" name="phone_no" value={this.state.phone_no} className="form-control" onChange={this.handleInputChange} />
</div>
<div className="clear"></div>
<div className="col_half">
<label htmlFor="register-form-password">Choose Password:</label>
<input type="password" id="register-form-password" name="password" value={this.state.password} className="form-control" onChange={this.handleInputChange} />
</div>
<div className="col_half col_last">
<label htmlFor="register-form-repassword">Re-enter Password:</label>
<input type="password" id="register-form-repassword" name="c_password" value={this.state.c_password} className="form-control" onChange={this.handleInputChange} />
</div>
<input type="hidden" name="user_role" value={this.state.user_role} className="form-control" />
<div className="clear"></div>
<div className="col_full nobottommargin">
<button className="button button-3d button-black nomargin" id="reg-submit" name="register-form-submit" value="register">Register Now</button>
</div>
</form>
</div>
</div>
</div>
</section>
</DefaultLayout>
)
}
}
LoginRegister.propTypes = {
userSignupRequest: PropTypes.func.isRequired
}
export default connect((state) => { return{} }, {userSignupRequest}) (LoginRegister);
和行动/ signupActions文件中有下面的代码,在调用API和使用Reducer
处理响应 -
import axios from 'axios';
export function userSignupRequest(userData){
return dispatch => {
dispatch({"type":"CALL_START"});
axios.post('../user/register', userData).then(response => {
dispatch({"type":"RECEIVE_RESPONSE", "payload":response.data});
}).catch(error => {
dispatch({"type":"RECEIVE_ERROR", "payload":error});
});
}
}
减速。 js有下面的代码 -
const reducer = (state={}, action) => {
switch(action.type){
case "CALL_START" :{
alert("Hi1");
return {}
break;
}
case "RECEIVE_RESPONSE" : {
//return {...state, data:action.payload}
alert("Hi2");
return {data:action.payload}
break;
}
case "RECEIVE_ERROR" : {
alert("Hi3");
alert(JSON.stringify(action.payload));
return {data:action.payload}
break;
}
}
}
export default reducer;
API正在调用,我得到的响应正常,但在这里我不知道如何显示成功/错误在我的'LoginRegister.js'组件上根据响应发送r消息。
请帮我解决这个问题。
您是否在使用提供程序,并且您是否使用了联合减速器组合了更多的减速器 –
是的我正在使用“提供程序”,但是现在我不使用“combineReducers”。 – Atul
您可以添加您的登录注册代码 –