gpt4 book ai didi

javascript - 提交表单后我无法 console.log() 用户详细信息?

转载 作者:行者123 更新时间:2023-12-03 01:47:54 24 4
gpt4 key购买 nike

我使用react-redux制作了银行详细信息提交表单。我无法 console.log(this.props.firstname) 等。在表单组件中使用 showUser() 方法。一旦我点击提交按钮,showUser()就会被触发。我将表单的输入值存储在 redux store 中,存储后我想要 console.log() 数据。

表单组件:

    import React, { Component } from 'react';
import { connect } from 'react-redux';
import actions 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);


}

setFirstName(event) {
this.props.dispatch(actions.setFirstName(event.target.value));
}

setLastName(event) {
this.props.dispatch(actions.setLastName(event.target.value));
}

setEmailId(event) {
this.props.dispatch(actions.setEmailId(event.target.value));
}

setIban(event) {
this.props.dispatch(actions.setIban(event.target.value));
}

setBankName(event) {
this.props.dispatch(actions.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" class="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" class="btn btn-success" onclick={this.showUser()}>Submit</button>
</div>


</form>
</div>
</div>

)}


}

const mapStateToProps = store => {
return {
firstname: store.user.firstname,
lastname: store.user.lastname,
emailid: store.user.emailid,
iban: store.user.bankaccounts.iban,
bankname: store.user.bankaccounts.bankname
}
}

export default connect(mapStateToProps)(Form);

reducer :

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}
}
}
}

}

export default userReducer;

行动:

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
}
}

商店:

import { createStore } from 'redux';

import userReducer from './reducers/reducers';


const store = createStore(userReducer);

store.subscribe(() => {
console.log('Store changed', store.getState());
})

export default store;

我在下面添加了表单检查的屏幕截图:

enter image description here

最佳答案

如果出现错误,在 ../actions/actions 中找不到导出默认值,请将导入语句更改为,

从'../actions/actions'导入{actions};

关于javascript - 提交表单后我无法 console.log() 用户详细信息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50551756/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com