Javascript 当自定义参数从子组件获取数据时,如何在React中除了传递您自己的自定义参数之外还传递事件参数

Javascript 当自定义参数从子组件获取数据时,如何在React中除了传递您自己的自定义参数之外还传递事件参数,javascript,reactjs,database,react-native,ecmascript-6,Javascript,Reactjs,Database,React Native,Ecmascript 6,我正在React中编程,并在parentTodoForm.js和childPrioritySelector.js组件之间传递数据。在本例中,我希望从子组件获取数据,因此我将自定义参数传递给函数handleSubmit,并将该函数作为propgetData={this.handleSubmit}传递给子组件。现在,我可以从子组件检索数据了,但是,当我想为e.preventDefault参数传递一个e参数时,它会给我一个错误。例如TypeError:e不是一个函数。有人知道我可以用同样的方法传递这两

我正在React中编程,并在parentTodoForm.js和childPrioritySelector.js组件之间传递数据。在本例中,我希望从子组件获取数据,因此我将自定义参数传递给函数handleSubmit,并将该函数作为propgetData={this.handleSubmit}传递给子组件。现在,我可以从子组件检索数据了,但是,当我想为e.preventDefault参数传递一个e参数时,它会给我一个错误。例如TypeError:e不是一个函数。有人知道我可以用同样的方法传递这两个参数scustom param和event param吗?非常感谢。我的代码如下。谢谢

import React from "react";
import PrioritySelector from "./PrioritySelector";
import { connect } from "react-redux";

class TodoForm extends React.Component {


    /*submit handler to grab input from the input references and store them
    in the "todoData" object. Then dispatches an action type and payload
    capturing the data. Then clears the input*/
    handleSubmit=( priorityLevelData, e)=> {
       e.preventDefault()
        const todoTitle = this.getTodoTitle.value;
        const description = this.getDescription.value;
        const priorityLevel = priorityLevelData;
        const todoData = {
            id: Math.floor(Math.random()*1000),
            todoTitle,
            description,
            priorityLevel,
            editing: false
        }
        this.props.dispatch({type:"ADD_TODO", todoData })
        this.getTodoTitle.value = "";
        this.getDescription.value = "";
    }

    render() {
        console.log(this.props, "TODOFORMPROPS")
        return(
            <div>
                <form onSubmit={this.handleSubmit}>
                    <input type="text" ref={(input)=> this.getTodoTitle=input} placeholder="Enter Todo" required/>
                    <input type="text" ref={(input)=> this.getDescription=input} placeholder="Enter Description" required/>
                    <PrioritySelector getData={this.handleSubmit} />
                    <button>Add Todo</button>
                </form>
            </div>
        )
    }
}



export default connect()(TodoForm);



    // PrioritySelector.js

import React from "react";
import $ from "jquery";
import { connect } from "react-redux";

class PrioritySelector extends React.Component  {


    componentDidMount() {
        $("#selector").show();
    }

    handleSelect =(e)=> {
       const priorityLevel = this.getPriorityLevel.value;
       this.props.getData(priorityLevel, e)
    }

    render() {
        console.log(this.props)
        return(
            <div>
                <div className="input-field col s12">
                    <select onChange={this.handleSelect} ref={(option)=> this.getPriorityLevel = option} id="selector">
                        <option disabled selected>Choose your option</option>
                        <option value="1">Low</option>
                        <option value="2">Medium</option>
                        <option value="3">High</option>
                    </select>
                </div>
            </div>
        )
    }

}


const mapStateToProps=(state)=> {
    return {
        priorityLevel: state
    }
}


export default connect(mapStateToProps)(PrioritySelector);

您忘记将事件传递给getData

onChange将允许您访问事件,但您仍必须将其传递给prop函数。 看起来是这样的:

handleSelect = e => {
  const priorityLevel = this.getPriorityLevel.value;
  this.props.getData(e, priorityLevel)
}
handleSubmit函数也应该交换它们的值:

handleSubmit=(e, priorityLevel)=> {
你看我把它们换了,让我解释一下原因

handleSubmit将由add按钮触发。这也将导致窗体触发handleSubmit函数。因此,该函数应该首先具有事件参数,然后可以添加所需的任何内容


您还应该向按钮添加type=submit,这将触发表单操作。

在子组件优先级选择器中,您有单击处理程序handleSelect。handleSelect在单击时将事件对象作为参数接收。您只需将此事件传递给this.props.getData props的父级

handleSelect = (event) => {
  const priorityLevel = this.getPriorityLevel.value;
  this.props.getData(priorityLevel, event);
}
父组件的handleSubmit现在可以访问从子组件传递的事件对象

handleSubmit = (priorityLevelData, e) => {
  // now has the event object e
  e.preventDefault()
  const todoTitle = this.getTodoTitle.value;
  const description = this.getDescription.value;
  const priorityLevel = priorityLevelData;
  const todoData = {

    id: Math.floor(Math.random() * 1000),
    todoTitle,
    description,
    priorityLevel,
    editing: false
  }
  this.props.dispatch({ type: "ADD_TODO", todoData })
  this.getTodoTitle.value = "";
  this.getDescription.value = "";
}

谢谢你的回复!嗨,我试过这种方法。现在它没有给我一个错误,但是它没有阻止默认操作。当用户选择优先级时,它会刷新页面。嘿,谢谢你的回复。我已将该事件添加到handleselect中,并对帖子进行了编辑。但是,现在它并没有阻止默认操作。正在刷新页面。请查看我的更改。谢谢页面现在没有刷新,但在我选择priorityLevel后,它会立即提交表单。有新的发现…哈哈。。。谢谢兄弟!