简体   繁体   English

Redux 表单道具未显示在事件处理程序中

[英]Redux Form props not displaying in event handler

I am building a very simple front end app to connect a few API's.我正在构建一个非常简单的前端应用程序来连接一些 API。 I have a redux form that should trigger an event handler which then triggers an action creator.我有一个 redux 表单,它应该触发一个事件处理程序,然后触发一个动作创建者。 The event handler is not receiving any props at present (just getting an empty object).事件处理程序目前没有接收任何道具(只是获取一个空对象)。 This is my component这是我的组件

import React, { Component } from 'react'
// import { Link } from 'react-router'
import { reduxForm } from 'redux-form'
import fetchConcerts from '../actions/fetchConcerts.js'

class ListContainer extends Component {

  handleFormSubmit(formProps){
    event.preventDefault()
    debugger
    this.props.fetchConcerts(formProps)

  }


  render(){
    const { fields: { userCity, userDate }, handleSubmit } = this.props;


        return (
            <div>
            <form onSubmit={handleSubmit(this.handleFormSubmit.bind(this))} >
                <label>City</label>
                <input type="text" className="form-input" {...userCity} />
                <label>Date</label>
                <input type="text" className="form-input" {...userDate} />
                <input type="submit" value="Submit" />
            </form>
            </div>
        );
      }
    }


export default reduxForm({
      form: 'search',
      fields: ['userCity', 'userDate']
    }, null, { fetchConcerts })(ListContainer);

App.js (the parent component) is as follow--> App.js(父组件)如下-->

import React, { Component } from 'react';
import ListContainer from './containers/ListContainer.js'
import './App.css';

export default class App extends Component {


  render() {
    return (
      <div className="App">
        <ListContainer />
      </div>
    );
  }
}

Upon submit, the handleFormSubmit is kicked off and I hit my debugger but formProps is Object = {}提交后,handleFormSubmit 被启动,我点击了调试器,但 formProps 是 Object = {}

Any ideas?有任何想法吗? I have run into this problem quite a bit with redux forms but I can never really figure out where I'm going wrong.我在使用 redux 表单时遇到了很多这个问题,但我永远无法真正弄清楚我哪里出错了。

Thanks谢谢

Are you trying to access the values entered in the input fields?您是否尝试访问输入字段中输入的值? If yes, this may be helpful:如果是,这可能会有所帮助:

handleFormSubmit(event) {
  event.preventDefault();

  // Access value and attributes of the City input
  console.log(event.target.elements[0].value);
  console.log(event.target.elements[0].attributes);

  // Access value and attributes of the Date input
  console.log(event.target.elements[1].value);
  console.log(event.target.elements[1].attributes);    
}

A working demo here: http://codepen.io/PiotrBerebecki/pen/vXXXaJ这里有一个工作演示: http : //codepen.io/PiotrBerebecki/pen/vXXXaJ

Although i haven't used redux-form , I think the issue is with your code, You can change your <form onSubmit={handleSubmit(this.handleFormSubmit.bind(this))} > to虽然我没有使用过redux-form ,但我认为问题在于您的代码,您可以将<form onSubmit={handleSubmit(this.handleFormSubmit.bind(this))} >更改为

<form
    onSubmit={ (event) => {
            handleSubmit( this.handleFormSubmit.bind(this, event, formProps) );
        }
    }
>

And your handleFormSubmit function to和你的handleFormSubmit函数

handleFormSubmit(event, formProps){
    event.preventDefault()
    debugger
    this.props.fetchConcerts(formProps)
}

EDIT #1编辑#1

After going through redux-form documentation, you could change the form to通过redux-form文档后,您可以将表单更改为

<form
    onSubmit={ (event) => {
            handleSubmit( data => { this.handleFormSubmit.bind(this, event, data) } );
        }
    }
>

And it should work.它应该工作。


And to simplify things even further并进一步简化事情

You can change your form component code to您可以将form component代码更改为

<form onSubmit={ handleSubmit( data => { this.handleFormSubmit.bind(this, data) } ) } >

And handleFormSubmit function tohandleFormSubmit函数

handleFormSubmit(data){
    console.log('form data', data);
    this.props.fetchConcerts(data)
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM