gpt4 book ai didi

javascript - react-redux 存储中的状态变量在 console.log 之后未定义?

转载 作者:行者123 更新时间:2023-11-30 20:29:10 25 4
gpt4 key购买 nike

我已经制作了银行帐户详细信息提交表单。当我在单击提交按钮后尝试 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);

Action .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;

截图: enter image description here

enter image description here

enter image description here

最佳答案

你的根 reducer 是 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
}
}

关于javascript - react-redux 存储中的状态变量在 console.log 之后未定义?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50552388/

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