我已提交银行帐户详细信息表单。当我在单击提交按钮后尝试console.log时,我无法捕获这些输入值并将其存储在redux store
中。在构造函数中使用console.log(this.props)
之后以及在使用console.log(this.props.firstname)
,console.log(this.props.lastname)
等之后,我在控制台中看到了未定义的内容,请参阅屏幕截图。
Form.js(表单组件)
import React, { Component } from 'react';
import { connect } from 'react-redux';
import * as action from '../actions/actions';
import './form.css';
class Form extends Component {
constructor(props) {
super(props)
this.setFirstName = this.setFirstName.bind(this);
this.setLastName = this.setLastName.bind(this);
this.setEmailId = this.setEmailId.bind(this);
this.setIban = this.setIban.bind(this);
this.setBankName = this.setBankName.bind(this);
this.showUser = this.showUser.bind(this);
console.log(this.props);
}
setFirstName(event) {
this.props.dispatch(action.setFirstName(event.target.value));
}
setLastName(event) {
this.props.dispatch(action.setLastName(event.target.value));
}
setEmailId(event) {
this.props.dispatch(action.setEmailId(event.target.value));
}
setIban(event) {
this.props.dispatch(action.setIban(event.target.value));
}
setBankName(event) {
this.props.dispatch(action.setBankName(event.target.value));
}
showUser(){
console.log(this.props.firstname);
console.log(this.props.lastname);
console.log(this.props.emailid);
}
render(){
return(
<div>
<div id="center">
<form>
<div className="form-group">
<label for="firstname">First Name:</label>
<input type="firstname" className="form-control" id="firstname" onChange={this.setFirstName}/>
</div>
<div className="form-group">
<label for="lastname">Last Name:</label>
<input type="lastname" className="form-control" id="lastname" onChange={this.setLastName}/>
</div>
<div className="form-group">
<label for="email">Email address:</label>
<input type="email" className="form-control" id="email" onChange={this.setEmailId}/>
</div>
<div className="form-group">
<label for="bankacc">IBAN:</label>
<div id="deletebank" className="items">
<input type="bankacc" className="form-control" id="bankacc" onChange={this.setIban}/>
<button type="button" className="btn btn-default btn-sm">
<span class="glyphicon glyphicon-trash"></span>
</button>
</div>
</div>
<div className="form-group">
<label for="bankname">Bank Name:</label>
<input type="bankname" className="form-control" id="bankname" onChange={this.setBankName}/>
</div>
<div className="form-group">
<button type="button" className="btn addbank">+ Add bank account</button>
</div>
<div className="form-group">
<button type="button" className="btn btn-success" onClick={this.showUser}>Submit</button>
</div>
</form>
</div>
</div>
)}
}
const mapStateToProps = store => {
return {
firstname: store.firstname,
lastname: store.lastname,
emailid: store.emailid,
iban: store.iban,
bankname: store.bankname
}
}
export default connect(mapStateToProps)(Form);
actions.js:
export const SET_FIRSTNAME = 'SET_FIRSTNAME';
export const SET_LASTNAME = 'SET_LASTNAME';
export const SET_EMAILID = 'SET_EMAILID';
export const SET_IBAN = 'SET_IBAN';
export const SET_BANKNAME = 'SET_BANKNAME';
export function setFirstName(firstname){
return {
type:SET_FIRSTNAME,
payLoad:firstname
}
}
export function setLastName(lastname){
return {
type:SET_LASTNAME,
payLoad:lastname
}
}
export function setEmailId(emailid){
return {
type:SET_EMAILID,
payLoad:emailid
}
}
export function setIban(iban){
return {
type:SET_IBAN,
payLoad:iban
}
}
export function setBankName(bankname){
return {
type:SET_BANKNAME,
payLoad:bankname
}
}
Reducer.js:
const userReducer = (state = {
user:{
firstname:'',
lastname:'',
emailid:'',
bankaccounts:{
iban:'',
bankname:''
}
}
}, action) => {
switch (action.type) {
case 'SET_FIRSTNAME':{
return {
...state,
user:{...state.user, firstname: action.payload}
}
}
case 'SET_LASTNAME':{
return {
...state,
user:{...state.user, lastname: action.payload}
}
}
case 'SET_EMAILID':{
return {
...state,
user:{...state.user, emailid: action.payload}
}
}
case 'SET_IBAN':{
return {
...state,
user:{...state.user, iban: action.payload}
}
}
case 'SET_BANKNAME':{
return {
...state,
user:{...state.user, bankname: action.payload}
}
}
default: return state;
}
}
export default userReducer;
答案 0 :(得分:1)
UserReducer
的根缩减器是具有属性user
的对象
在mapStateToProps
中,您应该像store.user.lastname
而不是store.lastname
const mapStateToProps = store => {
return {
firstname: store.user.firstname,
lastname: store.user.lastname,
emailid: store.user.emailid,
iban: store.user.iban,
bankname: store.user.bankname
}
}
答案 1 :(得分:0)
我认为你应该将它添加到构造函数中:
this.showUser = this.showUser.bind(this);