代码之家  ›  专栏  ›  技术社区  ›  stone rock

提交表单后,我无法控制台.log()用户详细信息?

  •  0
  • stone rock  · 技术社区  · 7 年前

    我已经用 react-redux . 我不能 console.log(this.props.firstname) 等。使用 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);
    

    减速器:

    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

    3 回复  |  直到 7 年前
        1
  •  1
  •   Rohith Murali    7 年前

    如果你有错误, export default was not found in ../actions/actions ,将导入语句更改为,

    import {actions} from '../actions/actions';

        2
  •  0
  •   siryu88    7 年前

    在用户还原程序中,您有用户对象,因此必须执行以下操作:

    console.log(this.props.user.firstname)
    

    而不是

    console.log(this.props.firstname)
    
        3
  •  0
  •   stone rock    7 年前

    表单组件应更改如下:

    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);
        }
    
    
      render(){
        return(
          <div>
              <div id="center">
                  <form>
                        <div className="form-group">
                             <label htmlFor="firstname">First Name:</label>
                             <input type="firstname" className="form-control" id="firstname" onChange={this.setFirstName}/>
                        </div>
    
                        <div className="form-group">
                             <label htmlFor="lastname">Last Name:</label>
                             <input type="lastname" className="form-control" id="lastname" onChange={this.setLastName}/>
                        </div>
    
                        <div className="form-group">
                            <label htmlFor="email">Email address:</label>
                            <input type="email" className="form-control" id="email" onChange={this.setEmailId}/>
                        </div>
    
                        <div className="form-group">
                             <label htmlFor="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 className="glyphicon glyphicon-trash"></span> 
                             </button>
                             </div>
                        </div>
    
                        <div className="form-group">
                             <label htmlFor="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.user.firstname,
        lastname: store.user.lastname,
        emailid: store.user.emailid,
        iban: store.user.iban,
        bankname: store.user.bankname
      }
    }
    
    export default connect(mapStateToProps)(Form);